[PATCH] [anayzer] Basic checker option validation

Gábor Horváth xazax.hun at gmail.com
Thu Mar 5 03:36:58 PST 2015


Hi zaks.anna,

This patch adds basic validation to checker options in the static analyzer.
This validation triggers a diagnostic error in case an option is set for a nonexistent checker or package.
It is not an error to set an option for a checker that is not enabled. I think disallowing that would be too restrictive and would have negative impact on user friendliness.
Once this validation is in place, the only one left is to make sure all package and checker names are identifiers (I think that check should be done at checker registration.) and
make sure the name of the option is an identifier as well.

http://reviews.llvm.org/D8077

Files:
  include/clang/StaticAnalyzer/Core/CheckerRegistry.h
  lib/StaticAnalyzer/Core/CheckerRegistry.cpp
  lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp

Index: include/clang/StaticAnalyzer/Core/CheckerRegistry.h
===================================================================
--- include/clang/StaticAnalyzer/Core/CheckerRegistry.h
+++ include/clang/StaticAnalyzer/Core/CheckerRegistry.h
@@ -64,6 +64,9 @@
 #endif
 
 namespace clang {
+class DiagnosticsEngine;
+class AnalyzerOptions;
+
 namespace ento {
 
 class CheckerOptInfo;
@@ -118,6 +121,10 @@
   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 ;
Index: lib/StaticAnalyzer/Core/CheckerRegistry.cpp
===================================================================
--- lib/StaticAnalyzer/Core/CheckerRegistry.cpp
+++ lib/StaticAnalyzer/Core/CheckerRegistry.cpp
@@ -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,26 @@
   }
 }
 
+void CheckerRegistry::validateCheckerOptions(const AnalyzerOptions &opts,
+                                             DiagnosticsEngine &diags) const {
+  for (auto &config : opts.Config) {
+    size_t pos = config.getKey().find(':');
+    if (pos != StringRef::npos) {
+      bool hasChecker = false;
+      StringRef optionName = config.getKey().substr(0, pos);
+      for (auto &checker : Checkers) {
+        if (checker.FullName.substr(0, pos) == optionName) {
+          hasChecker = true;
+          break;
+        }
+      }
+      if (!hasChecker) {
+        diags.Report(diag::err_unknown_analyzer_checker) << optionName;
+      }
+    }
+  }
+}
+
 void CheckerRegistry::printHelp(raw_ostream &out,
                                 size_t maxNameChars) const {
   // FIXME: Alphabetical sort puts 'experimental' in the middle.
Index: lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
===================================================================
--- lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
+++ lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
@@ -114,6 +114,7 @@
 
   ClangCheckerRegistry allCheckers(plugins, &diags);
   allCheckers.initializeManager(*checkerMgr, checkerOpts);
+  allCheckers.validateCheckerOptions(opts, diags);
   checkerMgr->finishedCheckerRegistration();
 
   for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8077.21267.patch
Type: text/x-patch
Size: 2979 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150305/3350f3be/attachment.bin>


More information about the cfe-commits mailing list