[PATCH] [Static Analyzer] Fix Analysis being skipped for code with declarations in .h file

Karthik Bhat kv.bhat at samsung.com
Mon Jun 1 00:34:06 PDT 2015


Hi zaks.anna, klimek,

Hi Anna,
This patch fixes a regression introduced by r224398. Prior to r224398 (e.g. in llvm 3.5) we were able to analyze the following code in test-include.c and report a null deref in this case. But post r224398 this analysis is being skipped as a result we miss many bugs in our codebase.
E.g.
  // test-include.c
  #include "test-include.h"
  void test(int * data) {
    data = 0;
    *data = 1;
  }

   // test-include.h
  void test(int * data);

This patch checks if the function declaration has a body in the mainfile currently being analyzed if yes then RunPathSensitiveChecks on the same. This fixes the above issue without any regressions.

Please let me know if you feel this patch looks good to you or if you feel there is a better way to fix the same.
Thanks and Regards
Karthik Bhat

http://reviews.llvm.org/D10156

Files:
  lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
  test/Analysis/test-include.c
  test/Analysis/test-include.h

Index: lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
===================================================================
--- lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
+++ lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
@@ -592,6 +592,11 @@
   if (!Opts->AnalyzeAll && !SM.isWrittenInMainFile(SL)) {
     if (SL.isInvalid() || SM.isInSystemHeader(SL))
       return AM_None;
+    // Check if the definition of the function declaration has a body.
+    // Return the current analysis mode if the definition is in the main file.
+    if (D->hasBody())
+      if (SM.isInMainFile(D->getBody()->getLocStart()))
+        return Mode;
     return Mode & ~AM_Path;
   }
 
Index: test/Analysis/test-include.c
===================================================================
--- test/Analysis/test-include.c
+++ test/Analysis/test-include.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify %s
+#include "test-include.h"
+void test(int * data) {
+  data = 0;
+  *data = 1; // expected-warning{{Dereference of null pointer}}
+}
Index: test/Analysis/test-include.h
===================================================================
--- test/Analysis/test-include.h
+++ test/Analysis/test-include.h
@@ -0,0 +1 @@
+void test(int * data);

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10156.26878.patch
Type: text/x-patch
Size: 1265 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150601/9bef4a25/attachment.bin>


More information about the cfe-commits mailing list