summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang-tools-extra/clang-tidy/ClangTidy.cpp21
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-macro-parentheses-cmdline.cpp10
2 files changed, 24 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp
index 9fbe0dd56b8..ffbe2499e08 100644
--- a/clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -128,13 +128,20 @@ public:
auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
<< Message.Message << Name;
for (const tooling::Replacement &Fix : Error.Fix) {
- SmallString<128> FixAbsoluteFilePath = Fix.getFilePath();
- Files.makeAbsolutePath(FixAbsoluteFilePath);
- SourceLocation FixLoc =
- getLocation(FixAbsoluteFilePath, Fix.getOffset());
- SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Fix.getLength());
- Diag << FixItHint::CreateReplacement(SourceRange(FixLoc, FixEndLoc),
- Fix.getReplacementText());
+ // Retrieve the source range for applicable fixes. Macro definitions
+ // on the command line have locations in a virtual buffer and don't
+ // have valid file paths and are therefore not applicable.
+ SourceRange Range;
+ SourceLocation FixLoc;
+ if (Fix.isApplicable()) {
+ SmallString<128> FixAbsoluteFilePath = Fix.getFilePath();
+ Files.makeAbsolutePath(FixAbsoluteFilePath);
+ FixLoc = getLocation(FixAbsoluteFilePath, Fix.getOffset());
+ SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Fix.getLength());
+ Range = SourceRange(FixLoc, FixEndLoc);
+ Diag << FixItHint::CreateReplacement(Range, Fix.getReplacementText());
+ }
+
++TotalFixes;
if (ApplyFixes) {
bool Success = Fix.isApplicable() && Fix.apply(Rewrite);
diff --git a/clang-tools-extra/test/clang-tidy/misc-macro-parentheses-cmdline.cpp b/clang-tools-extra/test/clang-tidy/misc-macro-parentheses-cmdline.cpp
new file mode 100644
index 00000000000..f7e80881070
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-macro-parentheses-cmdline.cpp
@@ -0,0 +1,10 @@
+// RUN: %check_clang_tidy %s misc-macro-parentheses %t -- -- -DVAL=0+0
+
+// The previous command-line is producing warnings and fixes with the source
+// locations from a virtual buffer. VAL is replaced by '0+0'.
+// Fixes could not be applied and should not be reported.
+int foo() { return VAL; }
+
+#define V 0+0
+int bar() { return V; }
+// CHECK-FIXES: #define V (0+0)
OpenPOWER on IntegriCloud