r241863 - [Static Analyzer] Basic per checker command line option validation.

Gabor Horvath xazax.hun at gmail.com
Thu Jul 9 14:43:45 PDT 2015


Author: xazax
Date: Thu Jul  9 16:43:45 2015
New Revision: 241863

URL: http://llvm.org/viewvc/llvm-project?rev=241863&view=rev
Log:
[Static Analyzer] Basic per checker command line option validation.

Differential Revision: http://reviews.llvm.org/D8077

Added:
    cfe/trunk/test/Analysis/analyzer-checker-config.c
Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/CheckerRegistry.h
    cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp
    cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/CheckerRegistry.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/CheckerRegistry.h?rev=241863&r1=241862&r2=241863&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/CheckerRegistry.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/CheckerRegistry.h Thu Jul  9 16:43:45 2015
@@ -64,6 +64,9 @@
 #endif
 
 namespace clang {
+class DiagnosticsEngine;
+class AnalyzerOptions;
+
 namespace ento {
 
 class CheckerOptInfo;
@@ -118,6 +121,10 @@ public:
   void initializeManager(CheckerManager &mgr,
                          SmallVectorImpl<CheckerOptInfo> &opts) const;
 
+  /// Check if every option corresponds to a specific checker or package.
+  void validateCheckerOptions(const AnalyzerOptions &opts,
+                              DiagnosticsEngine &diags) const;
+
   /// Prints the name and description of all checkers in this registry.
   /// This output is not intended to be machine-parseable.
   void printHelp(raw_ostream &out, size_t maxNameChars = 30) const ;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp?rev=241863&r1=241862&r2=241863&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp Thu Jul  9 16:43:45 2015
@@ -8,7 +8,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
+#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -111,6 +114,28 @@ void CheckerRegistry::initializeManager(
   }
 }
 
+void CheckerRegistry::validateCheckerOptions(const AnalyzerOptions &opts,
+                                             DiagnosticsEngine &diags) const {
+  for (auto &config : opts.Config) {
+    size_t pos = config.getKey().find(':');
+    if (pos == StringRef::npos)
+      continue;
+
+    bool hasChecker = false;
+    StringRef checkerName = config.getKey().substr(0, pos);
+    for (auto &checker : Checkers) {
+      if (checker.FullName.startswith(checkerName) &&
+          (checker.FullName.size() == pos || checker.FullName[pos] == '.')) {
+        hasChecker = true;
+        break;
+      }
+    }
+    if (!hasChecker) {
+      diags.Report(diag::err_unknown_analyzer_checker) << checkerName;
+    }
+  }
+}
+
 void CheckerRegistry::printHelp(raw_ostream &out,
                                 size_t maxNameChars) const {
   // FIXME: Alphabetical sort puts 'experimental' in the middle.

Modified: cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp?rev=241863&r1=241862&r2=241863&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp Thu Jul  9 16:43:45 2015
@@ -114,6 +114,7 @@ ento::createCheckerManager(AnalyzerOptio
 
   ClangCheckerRegistry allCheckers(plugins, &diags);
   allCheckers.initializeManager(*checkerMgr, checkerOpts);
+  allCheckers.validateCheckerOptions(opts, diags);
   checkerMgr->finishedCheckerRegistration();
 
   for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {

Added: cfe/trunk/test/Analysis/analyzer-checker-config.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/analyzer-checker-config.c?rev=241863&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/analyzer-checker-config.c (added)
+++ cfe/trunk/test/Analysis/analyzer-checker-config.c Thu Jul  9 16:43:45 2015
@@ -0,0 +1,12 @@
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config unix.mallo:Optimistic=true 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config uni:Optimistic=true 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config uni.:Optimistic=true 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config ..:Optimistic=true 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config unix.:Optimistic=true 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config unrelated:Optimistic=true 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-config unix.Malloc:Optimistic=true
+
+// Just to test clang is working.
+# foo
+
+// CHECK: error:





More information about the cfe-commits mailing list