r325975 - [analyzer] Consider switch- and goto- labels when constructing the set of executed lines

George Karpenkov via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 23 15:26:54 PST 2018


Author: george.karpenkov
Date: Fri Feb 23 15:26:54 2018
New Revision: 325975

URL: http://llvm.org/viewvc/llvm-project?rev=325975&view=rev
Log:
[analyzer] Consider switch- and goto- labels when constructing the set of executed lines

When viewing the report in the collapsed mode the label signifying where
did the execution go is often necessary for properly understanding the
context.

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

Added:
    cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/goto.c
    cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch.c
    cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch_default.c
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=325975&r1=325974&r2=325975&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/BugReporter.cpp Fri Feb 23 15:26:54 2018
@@ -3547,6 +3547,16 @@ static void populateExecutedLinesWithFun
     ExecutedLines->operator[](FID.getHashValue()).insert(Line);
 }
 
+static void populateExecutedLinesWithStmt(
+    const Stmt *S, SourceManager &SM,
+    std::unique_ptr<FilesToLineNumsMap> &ExecutedLines) {
+  SourceLocation Loc = S->getSourceRange().getBegin();
+  SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
+  FileID FID = SM.getFileID(ExpansionLoc);
+  unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
+  ExecutedLines->operator[](FID.getHashValue()).insert(LineNo);
+}
+
 /// \return all executed lines including function signatures on the path
 /// starting from \p N.
 static std::unique_ptr<FilesToLineNumsMap>
@@ -3567,13 +3577,21 @@ findExecutedLines(SourceManager &SM, con
       populateExecutedLinesWithFunctionSignature(D, SM, ExecutedLines);
 
     } else if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) {
+      populateExecutedLinesWithStmt(S, SM, ExecutedLines);
+
+      // Show extra context for some parent kinds.
+      const Stmt *P = N->getParentMap().getParent(S);
+
+      // The path exploration can die before the node with the associated
+      // return statement is generated, but we do want to show the whole
+      // return.
+      if (auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
+        populateExecutedLinesWithStmt(RS, SM, ExecutedLines);
+        P = N->getParentMap().getParent(RS);
+      }
 
-      // Otherwise: show lines associated with the processed statement.
-      SourceLocation Loc = S->getSourceRange().getBegin();
-      SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
-      FileID FID = SM.getFileID(ExpansionLoc);
-      unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
-      ExecutedLines->operator[](FID.getHashValue()).insert(LineNo);
+      if (P && (isa<SwitchCase>(P) || isa<LabelStmt>(P)))
+        populateExecutedLinesWithStmt(P, SM, ExecutedLines);
     }
 
     N = N->getFirstPred();

Added: cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/goto.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/goto.c?rev=325975&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/goto.c (added)
+++ cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/goto.c Fri Feb 23 15:26:54 2018
@@ -0,0 +1,13 @@
+int goto_test(int input) {
+  int *p = 0;
+  if (input)
+    goto mylabel;
+  return 0;
+mylabel:
+  return *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": {"1": 1,  "2": 1, "3": 1, "4": 1, "6": 1, "7": 1}};

Added: cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch.c?rev=325975&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch.c (added)
+++ cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch.c Fri Feb 23 15:26:54 2018
@@ -0,0 +1,20 @@
+enum E {
+  A, B, C
+};
+
+int f(enum E input) {
+  int *x = 0;
+  switch (input) {
+    case A:
+      return 1;
+    case B:
+      return 0;
+    case C:
+      return *x;
+  }
+}
+
+// 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": {"5": 1, "6": 1, "7": 1, "12": 1, "13": 1}};

Added: cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch_default.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch_default.c?rev=325975&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch_default.c (added)
+++ cfe/trunk/test/Analysis/html_diagnostics/relevant_lines/switch_default.c Fri Feb 23 15:26:54 2018
@@ -0,0 +1,20 @@
+enum E {
+  A, B, C
+};
+
+int f(enum E input) {
+  int *x = 0;
+  switch (input) {
+    case A:
+      return 1;
+    case B:
+      return 0;
+    default:
+      return *x;
+  }
+}
+
+// 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": {"5": 1, "6": 1, "7": 1, "12": 1, "13": 1}};




More information about the cfe-commits mailing list