Author: etienneb
Date: Tue Mar 22 12:51:27 2016
New Revision: 264073

URL: http://llvm.org/viewvc/llvm-project?rev=264073&view=rev
Log:
[clang-tidy] Skip reporting of not applicable fixes.

Summary:
Invalid source location are causing clang-tidy to crash when manipulating an 
invalid file.

Macro definitions on the command line have locations in a virtual buffer and 
therefore
don't have a corresponding valid FilePath.

A recent patch added path conversion to absolute path. As the FilePath may now 
be empty,
the result of makeAbsolutePath may incorrectly be the folder WorkingDir.  The 
crash occurs
in getLocation which is not able to find the appropriate FileEntry (null 
pointer).

```
          SmallString<128> FixAbsoluteFilePath = Fix.getFilePath();
          Files.makeAbsolutePath(FixAbsoluteFilePath);
          FixLoc = getLocation(FixAbsoluteFilePath, Fix.getOffset());
```

With relative path, the code was not crashing because getLocation was skipping 
empty path.



Example of code:

```
int main() { return X; }
```

With the given command-line:

```
clang-tidy test.cc --checks=misc-macro-*  --  -DX=0+0
```

Reviewers: alexfh, aaron.ballman

Subscribers: aaron.ballman, cfe-commits

Differential Revision: http://reviews.llvm.org/D18262

Added:
    clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=264073&r1=264072&r2=264073&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Tue Mar 22 12:51:27 2016
@@ -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);

Added: 
clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp?rev=264073&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp 
(added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp 
Tue Mar 22 12:51:27 2016
@@ -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)


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to