r341618 - [analyzer] Do not add invalid source location to the coverage information

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 6 17:43:17 PDT 2018


Author: george.karpenkov
Date: Thu Sep  6 17:43:17 2018
New Revision: 341618

URL: http://llvm.org/viewvc/llvm-project?rev=341618&view=rev
Log:
[analyzer] Do not add invalid source location to the coverage information

Invalid source locations may arise from generated code.

Differential Revision: https://reviews.llvm.org/D51761

Added:
    cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp
Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp?rev=341618&r1=341617&r2=341618&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp Thu Sep  6 17:43:17 2018
@@ -1892,6 +1892,7 @@ static void updateExecutedLinesWithDiagn
     FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc();
     FileID FID = Loc.getFileID();
     unsigned LineNo = Loc.getLineNumber();
+    assert(FID.isValid());
     ExecutedLines[FID.getHashValue()].insert(LineNo);
   }
 }
@@ -3022,6 +3023,8 @@ static void populateExecutedLinesWithFun
   SourceLocation Start = SignatureSourceRange.getBegin();
   SourceLocation End = Body ? Body->getSourceRange().getBegin()
     : SignatureSourceRange.getEnd();
+  if (!Start.isValid() || !End.isValid())
+    return;
   unsigned StartLine = SM.getExpansionLineNumber(Start);
   unsigned EndLine = SM.getExpansionLineNumber(End);
 
@@ -3034,6 +3037,8 @@ static void populateExecutedLinesWithStm
     const Stmt *S, SourceManager &SM,
     std::unique_ptr<FilesToLineNumsMap> &ExecutedLines) {
   SourceLocation Loc = S->getSourceRange().getBegin();
+  if (!Loc.isValid())
+    return;
   SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
   FileID FID = SM.getFileID(ExpansionLoc);
   unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);

Added: cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp?rev=341618&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp (added)
+++ cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp Thu Sep  6 17:43:17 2018
@@ -0,0 +1,25 @@
+// Faking std::call_once implementation.
+namespace std {
+typedef struct once_flag_s {
+  int _M_once = 0;
+} once_flag;
+
+template <class Callable, class... Args>
+void call_once(once_flag &o, Callable&& func, Args&&... args);
+} // namespace std
+
+int deref(int *x) {
+  return *x;
+}
+
+void call_deref_once() {
+  static std::once_flag once;
+  int *p = nullptr;
+  std::call_once(once, &deref, p);
+}
+
+
+// RUN: rm -rf %t.output
+// RUN: %clang_analyze_cc1 -analyze -analyzer-checker=core -analyzer-output html -o %t.output %s
+// RUN: cat %t.output/* | FileCheck %s --match-full-lines
+// CHECK: var relevant_lines = {"1": {"3": 1,  "8": 1, "11": 1, "12": 1, "15": 1, "16": 1, "17": 1, "18": 1}};




More information about the cfe-commits mailing list