Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clang/test/Format/dry-run-warning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: echo '{' > %t.json
// RUN: echo ' "married": true' >> %t.json
// RUN: echo '}' >> %t.json

// RUN: clang-format -n -style=LLVM %t.json 2>&1 | FileCheck %s -allow-empty

// RUN: clang-format -n -style=LLVM < %t.json 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK2 -strict-whitespace

// RUN: echo '{' > %t.json
// RUN: echo ' "married" : true' >> %t.json
// RUN: echo '}' >> %t.json

// RUN: clang-format -n -style=LLVM < %t.json 2>&1 | FileCheck %s -allow-empty

// RUN: clang-format -n -style=LLVM %t.json 2>&1 \
// RUN: | FileCheck %s -check-prefix=CHECK2 -strict-whitespace

// RUN: rm %t.json

// CHECK-NOT: warning
// CHECK2: warning: code should be clang-formatted
13 changes: 7 additions & 6 deletions clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ static void outputReplacementsXML(const Replacements &Replaces) {
static bool
emitReplacementWarnings(const Replacements &Replaces, StringRef AssumedFileName,
const std::unique_ptr<llvm::MemoryBuffer> &Code) {
if (Replaces.empty())
return false;

unsigned Errors = 0;
if (WarnFormat && !NoWarnFormat) {
SourceMgr Mgr;
Expand Down Expand Up @@ -490,9 +487,11 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
AssumedFileName, &CursorPosition);

const bool IsJson = FormatStyle->isJson();

// To format JSON insert a variable to trick the code into thinking its
// JavaScript.
if (FormatStyle->isJson() && !FormatStyle->DisableFormat) {
if (IsJson && !FormatStyle->DisableFormat) {
auto Err = Replaces.add(tooling::Replacement(
tooling::Replacement(AssumedFileName, 0, 0, "x = ")));
if (Err)
Expand All @@ -511,8 +510,10 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
reformat(*FormatStyle, *ChangedCode, Ranges, AssumedFileName, &Status);
Replaces = Replaces.merge(FormatChanges);
if (OutputXML || DryRun) {
if (DryRun)
return emitReplacementWarnings(Replaces, AssumedFileName, Code);
if (DryRun) {
return Replaces.size() > (IsJson ? 1 : 0) &&
emitReplacementWarnings(Replaces, AssumedFileName, Code);
}
outputXML(Replaces, FormatChanges, Status, Cursor, CursorPosition);
} else {
IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
Expand Down
Loading