[clang-tools-extra] r202987 - Fixed a crash when handling diagnostics without a valid file location, e.g. 'error reading <file>'.

Alexander Kornienko alexfh at google.com
Wed Mar 5 07:44:36 PST 2014


Author: alexfh
Date: Wed Mar  5 09:44:36 2014
New Revision: 202987

URL: http://llvm.org/viewvc/llvm-project?rev=202987&view=rev
Log:
Fixed a crash when handling diagnostics without a valid file location, e.g. 'error reading <file>'.

Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
    clang-tools-extra/trunk/test/clang-tidy/diagnostic.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp?rev=202987&r1=202986&r2=202987&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyDiagnosticConsumer.cpp Wed Mar  5 09:44:36 2014
@@ -110,7 +110,7 @@ void ClangTidyDiagnosticConsumer::Handle
   addFixes(Info, Errors.back());
 
   // Let argument parsing-related warnings through.
-  if (!Diags->hasSourceManager() ||
+  if (!Info.getLocation().isValid() ||
       !Diags->getSourceManager().isInSystemHeader(Info.getLocation())) {
     LastErrorRelatesToUserCode = true;
   }
@@ -128,12 +128,16 @@ void ClangTidyDiagnosticConsumer::addFix
                                            ClangTidyError &Error) {
   if (!Info.hasSourceManager())
     return;
-  SourceManager &SourceMgr = Info.getSourceManager();
+  SourceManager &Sources = Info.getSourceManager();
   tooling::Replacements Replacements;
   for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) {
-    Error.Fix.insert(tooling::Replacement(
-        SourceMgr, Info.getFixItHint(i).RemoveRange.getBegin(), 0,
-        Info.getFixItHint(i).CodeToInsert));
+    CharSourceRange Range = Info.getFixItHint(i).RemoveRange;
+    assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
+           "Invalid range in the fix-it hint.");
+    assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
+           "Only file locations supported in fix-it hints.");
+    std::string Text = Info.getFixItHint(i).CodeToInsert;
+    Error.Fix.insert(tooling::Replacement(Sources, Range, Text));
   }
 }
 

Modified: clang-tools-extra/trunk/test/clang-tidy/diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/diagnostic.cpp?rev=202987&r1=202986&r2=202987&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/diagnostic.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/diagnostic.cpp Wed Mar  5 09:44:36 2014
@@ -1,5 +1,5 @@
-// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
-// RUN: clang-tidy %t.cpp -- -fan-unknown-option > %t2.cpp
-// RUN: FileCheck -input-file=%t2.cpp %s
+// RUN: clang-tidy %s -- -fan-unknown-option | FileCheck -check-prefix=CHECK1 %s
+// RUN: clang-tidy %s.nonexistent.cpp -- | FileCheck -check-prefix=CHECK2 %s
 
-// CHECK: warning: unknown argument: '-fan-unknown-option'
+// CHECK1: warning: unknown argument: '-fan-unknown-option'
+// CHECK2: warning: error reading '{{.*}}.nonexistent.cpp'





More information about the cfe-commits mailing list