[PATCH] D18262: [clang-tidy] Skip reporting of not applicable fixes.

Etienne Bergeron via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 17 20:25:05 PDT 2016


etienneb created this revision.
etienneb added reviewers: rnk, emso, alexfh, bkramer.
etienneb added a subscriber: cfe-commits.

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
```

http://reviews.llvm.org/D18262

Files:
  ClangTidy.cpp

Index: ClangTidy.cpp
===================================================================
--- ClangTidy.cpp
+++ ClangTidy.cpp
@@ -128,13 +128,20 @@
       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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18262.51010.patch
Type: text/x-patch
Size: 1641 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160318/e618883e/attachment-0001.bin>


More information about the cfe-commits mailing list