From 55e3d1ec353a5849a0c3da72faa44cec6d528dfa Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Fri, 7 Sep 2018 00:43:17 +0000 Subject: [PATCH] [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 llvm-svn: 341618 --- clang/lib/StaticAnalyzer/Core/BugReporter.cpp | 5 +++++ .../relevant_lines/synthesized_body.cpp | 25 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 clang/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp diff --git a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp index 19e9b4a..4e0a0d5 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -1892,6 +1892,7 @@ static void updateExecutedLinesWithDiagnosticPieces( 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 populateExecutedLinesWithFunctionSignature( 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 populateExecutedLinesWithStmt( const Stmt *S, SourceManager &SM, std::unique_ptr &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); diff --git a/clang/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp b/clang/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp new file mode 100644 index 0000000..a4e30e3 --- /dev/null +++ b/clang/test/Analysis/html_diagnostics/relevant_lines/synthesized_body.cpp @@ -0,0 +1,25 @@ +// Faking std::call_once implementation. +namespace std { +typedef struct once_flag_s { + int _M_once = 0; +} once_flag; + +template +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}}; -- 2.7.4