]> granicus.if.org Git - clang/commitdiff
[analyzer] CStringChecker: Remember to highlight the argument expression range.
authorArtem Dergachev <artem.dergachev@gmail.com>
Mon, 30 Jul 2018 23:44:37 +0000 (23:44 +0000)
committerArtem Dergachev <artem.dergachev@gmail.com>
Mon, 30 Jul 2018 23:44:37 +0000 (23:44 +0000)
When emitting a bug report, it is important to highlight which argument of the
call-expression is causing the problem.

Before:
warning: Null pointer argument in call to string comparison function
  strcmp(a, b);
  ^~~~~~~~~~~~

After:
warning: Null pointer argument in call to string comparison function
  strcmp(a, b);
  ^      ~

Affects other output modes as well, not just text.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338333 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/CStringChecker.cpp
test/Analysis/cstring-ranges.c [new file with mode: 0644]

index 278452ec994a69d6dfc1097fb472371cfc1a559c..12a576e5d80d549ea69e123f31b788fe6ba8d830 100644 (file)
@@ -552,6 +552,7 @@ void CStringChecker::emitNullArgBug(CheckerContext &C, ProgramStateRef State,
 
     BuiltinBug *BT = static_cast<BuiltinBug *>(BT_Null.get());
     auto Report = llvm::make_unique<BugReport>(*BT, WarningMsg, N);
+    Report->addRange(S->getSourceRange());
     bugreporter::trackNullOrUndefValue(N, S, *Report);
     C.emitReport(std::move(Report));
   }
diff --git a/test/Analysis/cstring-ranges.c b/test/Analysis/cstring-ranges.c
new file mode 100644 (file)
index 0000000..4fcd7ea
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=unix.cstring -analyzer-output=text %s 2>&1 | FileCheck %s
+
+// This test verifies argument source range highlighting.
+// Otherwise we've no idea which of the arguments is null.
+
+char *strcpy(char *, const char *);
+
+void foo() {
+  char *a = 0, *b = 0;
+  strcpy(a, b);
+}
+
+// CHECK: warning: Null pointer argument in call to string copy function
+// CHECK-NEXT: strcpy(a, b);
+// CHECK-NEXT: ^      ~