[PATCH] D54397: [analyzer][NFC] Move CheckerOptInfo to CheckerRegistry.cpp, and make it local

2018-11-18 Thread Umann Kristóf via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL347157: [analyzer][NFC] Move CheckerOptInfo to 
CheckerRegistry.cpp, and make it local (authored by Szelethus, committed by ).
Herald added subscribers: llvm-commits, gamesh411, baloghadamsoftware.

Changed prior to commit:
  https://reviews.llvm.org/D54397?vs=173568=174535#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D54397

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

Index: cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
===
--- cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
@@ -17,7 +17,6 @@
 #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
-#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
 #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
 #include "llvm/ADT/SmallVector.h"
@@ -102,44 +101,23 @@
   << pluginAPIVersion;
 }
 
-static SmallVector
-getCheckerOptList(const AnalyzerOptions ) {
-  SmallVector checkerOpts;
-  for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
-const std::pair  = opts.CheckersControlList[i];
-checkerOpts.push_back(CheckerOptInfo(opt.first, opt.second));
-  }
-  return checkerOpts;
-}
-
 std::unique_ptr ento::createCheckerManager(
 ASTContext ,
 AnalyzerOptions ,
 ArrayRef plugins,
 ArrayRef> checkerRegistrationFns,
 DiagnosticsEngine ) {
   auto checkerMgr = llvm::make_unique(context, opts);
 
-  SmallVector checkerOpts = getCheckerOptList(opts);
-
   ClangCheckerRegistry allCheckers(plugins, );
 
   for (const auto  : checkerRegistrationFns)
 Fn(allCheckers);
 
-  allCheckers.initializeManager(*checkerMgr, checkerOpts);
+  allCheckers.initializeManager(*checkerMgr, opts, diags);
   allCheckers.validateCheckerOptions(opts, diags);
   checkerMgr->finishedCheckerRegistration();
 
-  for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
-if (checkerOpts[i].isUnclaimed()) {
-  diags.Report(diag::err_unknown_analyzer_checker)
-  << checkerOpts[i].getName();
-  diags.Report(diag::note_suggest_disabling_all_checkers);
-}
-
-  }
-
   return checkerMgr;
 }
 
@@ -155,8 +133,7 @@
const AnalyzerOptions ) {
   out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
 
-  SmallVector checkerOpts = getCheckerOptList(opts);
-  ClangCheckerRegistry(plugins).printList(out, checkerOpts);
+  ClangCheckerRegistry(plugins).printList(out, opts);
 }
 
 void ento::printAnalyzerConfigList(raw_ostream ) {
Index: cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp
===
--- cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Core/CheckerRegistry.cpp
@@ -12,7 +12,6 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
-#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetVector.h"
@@ -30,6 +29,41 @@
 
 using CheckerInfoSet = llvm::SetVector;
 
+namespace {
+/// Represents a request to include or exclude a checker or package from a
+/// specific analysis run.
+///
+/// \sa CheckerRegistry::initializeManager
+class CheckerOptInfo {
+  StringRef Name;
+  bool Enable;
+  bool Claimed;
+
+public:
+  CheckerOptInfo(StringRef name, bool enable)
+: Name(name), Enable(enable), Claimed(false) { }
+
+  StringRef getName() const { return Name; }
+  bool isEnabled() const { return Enable; }
+  bool isDisabled() const { return !isEnabled(); }
+
+  bool isClaimed() const { return Claimed; }
+  bool isUnclaimed() const { return !isClaimed(); }
+  void claim() { Claimed = true; }
+};
+
+} // end of anonymous namespace
+
+static SmallVector
+getCheckerOptList(const AnalyzerOptions ) {
+  SmallVector checkerOpts;
+  for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
+const std::pair  = opts.CheckersControlList[i];
+checkerOpts.push_back(CheckerOptInfo(opt.first, opt.second));
+  }
+  return checkerOpts;
+}
+
 static bool checkerNameLT(const CheckerRegistry::CheckerInfo ,
   const CheckerRegistry::CheckerInfo ) {
   return a.FullName < b.FullName;
@@ -52,6 +86,7 @@
   return false;
 }
 
+/// Collects the checkers for the supplied \p opt option into \p collected.
 static void collectCheckers(const 

[PATCH] D54397: [analyzer][NFC] Move CheckerOptInfo to CheckerRegistry.cpp, and make it local

2018-11-11 Thread Umann Kristóf via Phabricator via cfe-commits
Szelethus created this revision.
Szelethus added reviewers: NoQ, george.karpenkov, xazax.hun, rnkovacs, MTC.
Herald added subscribers: cfe-commits, dkrupp, donat.nagy, mikhail.ramalho, 
a.sidorin, szepet, whisperity.

TL;DR: `CheckerOptInfo` feels very much out of place in 
`CheckerRegistration.cpp`, so I moved it to `CheckerRegistry.h`.

Details:

While on the quest of fixing checker options the same way I cleaned up 
non-checker options, although I'm making good progress, I ran into a wall: In 
order to emit warnings on incorrect checker options, we need to ensure that 
checkers actually acquire their options properly -- but, I unearthed a huge bug 
in checker registration: dependencies are currently implemented in a way that 
breaks the already very fragile registration infrastructure.

Here is where the problem really originates from: this is a snippet from 
`CheckerRegistry::initializeManager`.

  // Initialize the CheckerManager with all enabled checkers.
  for (const auto *i :enabledCheckers) {
checkerMgr.setCurrentCheckName(CheckName(i->FullName));
i->Initialize(checkerMgr);
  }

Note that `Initialize` is a function pointer to `register##CHECKERNAME`, like 
`registerInnerPointerChecker`. Since for each register function call the 
current checker name is only set once, when `InnerPointerChecker` attempts to 
also register it's dependent `MallocChecker`, both it and `MallocChecker` will 
receive the `cplusplus.InnerPointer` name. This results in `MallocChecker` 
trying to acquire it's `Optimistic` option as 
`cplusplus.InnerPointerChecker:Optimistic`.

Clearly, this problem has to be solved at a higher level -- it makes no sense 
to be affect other checkers in a registry function. Since I'll already make 
changes to how checker registration works, I'll probably tune some things for 
the current C++ standard, add much needed documentation, and try to make the 
code //a lot less confusing//.


Repository:
  rC Clang

https://reviews.llvm.org/D54397

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

Index: lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
===
--- lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
+++ lib/StaticAnalyzer/Frontend/CheckerRegistration.cpp
@@ -17,7 +17,6 @@
 #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
-#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
 #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
 #include "llvm/ADT/SmallVector.h"
@@ -102,44 +101,23 @@
   << pluginAPIVersion;
 }
 
-static SmallVector
-getCheckerOptList(const AnalyzerOptions ) {
-  SmallVector checkerOpts;
-  for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
-const std::pair  = opts.CheckersControlList[i];
-checkerOpts.push_back(CheckerOptInfo(opt.first, opt.second));
-  }
-  return checkerOpts;
-}
-
 std::unique_ptr ento::createCheckerManager(
 ASTContext ,
 AnalyzerOptions ,
 ArrayRef plugins,
 ArrayRef> checkerRegistrationFns,
 DiagnosticsEngine ) {
   auto checkerMgr = llvm::make_unique(context, opts);
 
-  SmallVector checkerOpts = getCheckerOptList(opts);
-
   ClangCheckerRegistry allCheckers(plugins, );
 
   for (const auto  : checkerRegistrationFns)
 Fn(allCheckers);
 
-  allCheckers.initializeManager(*checkerMgr, checkerOpts);
+  allCheckers.initializeManager(*checkerMgr, opts, diags);
   allCheckers.validateCheckerOptions(opts, diags);
   checkerMgr->finishedCheckerRegistration();
 
-  for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
-if (checkerOpts[i].isUnclaimed()) {
-  diags.Report(diag::err_unknown_analyzer_checker)
-  << checkerOpts[i].getName();
-  diags.Report(diag::note_suggest_disabling_all_checkers);
-}
-
-  }
-
   return checkerMgr;
 }
 
@@ -155,8 +133,7 @@
const AnalyzerOptions ) {
   out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
 
-  SmallVector checkerOpts = getCheckerOptList(opts);
-  ClangCheckerRegistry(plugins).printList(out, checkerOpts);
+  ClangCheckerRegistry(plugins).printList(out, opts);
 }
 
 void ento::printAnalyzerConfigList(raw_ostream ) {
Index: lib/StaticAnalyzer/Core/CheckerRegistry.cpp
===
--- lib/StaticAnalyzer/Core/CheckerRegistry.cpp
+++ lib/StaticAnalyzer/Core/CheckerRegistry.cpp
@@ -12,7 +12,6 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
-#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
 #include