[cfe-commits] r167337 - in /cfe/trunk: include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp lib/StaticAnalyzer/Core/CheckerContext.cpp

Jordan Rose jordan_rose at apple.com
Fri Nov 2 16:49:24 PDT 2012


Author: jrose
Date: Fri Nov  2 18:49:24 2012
New Revision: 167337

URL: http://llvm.org/viewvc/llvm-project?rev=167337&view=rev
Log:
[analyzer] isCLibraryFunction: check that the function is at TU-scope.

Also, Decls already carry a pointer to the ASTContext, so there's no need
to pass an extra argument to the predicate.

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
    cfe/trunk/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h?rev=167337&r1=167336&r2=167337&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h Fri Nov  2 18:49:24 2012
@@ -212,11 +212,9 @@
     return getCalleeName(FunDecl);
   }
 
-  /// Given a function declaration and a name checks if this is a C lib
-  /// function with the given name.
-  bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name);
-  static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name,
-                                 ASTContext &Context);
+  /// \brief Returns true if the given function is the specified built-in or
+  /// system library C function.
+  static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name);
 
   /// \brief Depending on wither the location corresponds to a macro, return 
   /// either the macro name or the token spelling.

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp?rev=167337&r1=167336&r2=167337&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/CStringSyntaxChecker.cpp Fri Nov  2 18:49:24 2012
@@ -33,7 +33,6 @@
 class WalkAST: public StmtVisitor<WalkAST> {
   BugReporter &BR;
   AnalysisDeclContext* AC;
-  ASTContext &ASTC;
 
   /// Check if two expressions refer to the same declaration.
   inline bool sameDecl(const Expr *A1, const Expr *A2) {
@@ -58,8 +57,8 @@
       const FunctionDecl *FD = CE->getDirectCallee();
       if (!FD)
         return false;
-      return (CheckerContext::isCLibraryFunction(FD, "strlen", ASTC)
-          && sameDecl(CE->getArg(0), WithArg));
+      return (CheckerContext::isCLibraryFunction(FD, "strlen") &&
+              sameDecl(CE->getArg(0), WithArg));
     }
     return false;
   }
@@ -83,7 +82,7 @@
 
 public:
   WalkAST(BugReporter &br, AnalysisDeclContext* ac) :
-      BR(br), AC(ac), ASTC(AC->getASTContext()) {
+      BR(br), AC(ac) {
   }
 
   // Statement visitor methods.
@@ -136,7 +135,7 @@
   if (!FD)
     return;
 
-  if (CheckerContext::isCLibraryFunction(FD, "strncat", ASTC)) {
+  if (CheckerContext::isCLibraryFunction(FD, "strncat")) {
     if (containsBadStrncatPattern(CE)) {
       const Expr *DstArg = CE->getArg(0);
       const Expr *LenArg = CE->getArg(2);

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp?rev=167337&r1=167336&r2=167337&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CheckerContext.cpp Fri Nov  2 18:49:24 2012
@@ -38,17 +38,12 @@
 
 bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
                                         StringRef Name) {
-  return isCLibraryFunction(FD, Name, getASTContext());
-}
-
-bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
-                                        StringRef Name, ASTContext &Context) {
   // To avoid false positives (Ex: finding user defined functions with
   // similar names), only perform fuzzy name matching when it's a builtin.
   // Using a string compare is slow, we might want to switch on BuiltinID here.
   unsigned BId = FD->getBuiltinID();
   if (BId != 0) {
-    StringRef BName = Context.BuiltinInfo.GetName(BId);
+    StringRef BName = FD->getASTContext().BuiltinInfo.GetName(BId);
     if (BName.find(Name) != StringRef::npos)
       return true;
   }
@@ -59,6 +54,19 @@
   if (!II)
     return false;
 
+  // Look through 'extern "C"' and anything similar invented in the future.
+  const DeclContext *DC = FD->getDeclContext();
+  while (DC->isTransparentContext())
+    DC = DC->getParent();
+
+  // If this function is in a namespace, it is not a C library function.
+  if (!DC->isTranslationUnit())
+    return false;
+
+  // If this function is not externally visible, it is not a C library function.
+  if (FD->getLinkage() != ExternalLinkage)
+    return false;
+
   StringRef FName = II->getName();
   if (FName.equals(Name))
     return true;





More information about the cfe-commits mailing list