leanil created this revision.
Herald added a subscriber: JDevlieghere.
Clang warnings can be mapped to virtual check names as aliases. The mappings
can be set in the appropriate ClangTidyModules. If the virtual check is
enabled, then
- the corresponding warning options get passed to clang, and
- the warning names are replaced with the check alias in the produced messages.
This is an implementation of the proposal here
<http://clang-developers.42468.n3.nabble.com/RFC-clang-tidy-Register-warnings-as-check-aliases-td4053573.html>.
https://reviews.llvm.org/D38171
Files:
clang-tidy/ClangTidy.cpp
clang-tidy/ClangTidyDiagnosticConsumer.cpp
clang-tidy/ClangTidyDiagnosticConsumer.h
clang-tidy/ClangTidyModule.h
clang-tidy/cert/CERTTidyModule.cpp
test/clang-tidy/warning-check-aliases.cpp
Index: test/clang-tidy/warning-check-aliases.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/warning-check-aliases.cpp
@@ -0,0 +1,20 @@
+// RUN: clang-tidy %s -- 2>&1 | FileCheck -implicit-check-not='{{warning:|error:}}' %s
+// RUN: clang-tidy %s -checks='-*,cert-err54-cpp' -- 2>&1 | FileCheck -implicit-check-not='{{warning:|error:}}' -check-prefix=CHECK2 %s
+
+class B {};
+class D : public B {};
+
+void f() {
+ try {
+
+ } catch (B &X) {
+
+ } catch (D &Y) {
+ }
+}
+
+//CHECK: :12:12: warning: exception of type 'D &' will be caught by earlier handler [clang-diagnostic-exceptions]
+//CHECK: :10:12: note: for type 'B &'
+
+//CHECK2: :12:12: warning: exception of type 'D &' will be caught by earlier handler [cert-err54-cpp]
+//CHECK2: :10:12: note: for type 'B &'
Index: clang-tidy/cert/CERTTidyModule.cpp
===================================================================
--- clang-tidy/cert/CERTTidyModule.cpp
+++ clang-tidy/cert/CERTTidyModule.cpp
@@ -26,6 +26,7 @@
#include "StrToNumCheck.h"
#include "ThrownExceptionTypeCheck.h"
#include "VariadicFunctionDefCheck.h"
+#include "clang/Sema/SemaDiagnostic.h"
namespace clang {
namespace tidy {
@@ -73,6 +74,14 @@
// MSC
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
}
+
+ void addWarningCheckAliases(
+ llvm::DenseMap<unsigned, llvm::StringRef> &WarningCheckAliases) {
+ WarningCheckAliases.insert(
+ {diag::warn_exception_caught_by_earlier_handler, "cert-err54-cpp"});
+ WarningCheckAliases.insert(
+ {diag::ext_offsetof_non_standardlayout_type, "cert-exp59-cpp"});
+ }
};
} // namespace cert
Index: clang-tidy/ClangTidyModule.h
===================================================================
--- clang-tidy/ClangTidyModule.h
+++ clang-tidy/ClangTidyModule.h
@@ -11,6 +11,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
#include "ClangTidy.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include <functional>
#include <map>
@@ -89,6 +90,11 @@
/// belonging to this module.
virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
+ /// \brief Implement this function in order to register all warning-check
+ /// aliases belonging to this module.
+ virtual void addWarningCheckAliases(
+ llvm::DenseMap<unsigned, llvm::StringRef> &WarningCheckAliases) {}
+
/// \brief Gets default options for checks defined in this module.
virtual ClangTidyOptions getModuleOptions();
};
Index: clang-tidy/ClangTidyDiagnosticConsumer.h
===================================================================
--- clang-tidy/ClangTidyDiagnosticConsumer.h
+++ clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -17,6 +17,7 @@
#include "clang/Tooling/Refactoring.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Timer.h"
@@ -186,6 +187,8 @@
return CurrentBuildDirectory;
}
+ llvm::DenseMap<unsigned, llvm::StringRef> WarningCheckAliases;
+
private:
// Calls setDiagnosticsEngine() and storeError().
friend class ClangTidyDiagnosticConsumer;
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -374,9 +374,15 @@
StringRef WarningOption =
Context.DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(
Info.getID());
- std::string CheckName = !WarningOption.empty()
- ? ("clang-diagnostic-" + WarningOption).str()
- : Context.getCheckName(Info.getID()).str();
+ auto Alias = Context.WarningCheckAliases.find(Info.getID());
+ std::string CheckName;
+ if (Alias != Context.WarningCheckAliases.end() &&
+ Context.isCheckEnabled(Alias->second))
+ CheckName = Alias->second;
+ else
+ CheckName = !WarningOption.empty()
+ ? ("clang-diagnostic-" + WarningOption).str()
+ : Context.getCheckName(Info.getID()).str();
if (CheckName.empty()) {
// This is a compiler diagnostic without a warning option. Assign check
Index: clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tidy/ClangTidy.cpp
+++ clang-tidy/ClangTidy.cpp
@@ -286,6 +286,7 @@
I != E; ++I) {
std::unique_ptr<ClangTidyModule> Module(I->instantiate());
Module->addCheckFactories(*CheckFactories);
+ Module->addWarningCheckAliases(Context.WarningCheckAliases);
}
}
@@ -397,6 +398,10 @@
CheckNames.push_back(CheckFactory.first);
}
+ for (const auto &Alias : Context.WarningCheckAliases)
+ if (Context.isCheckEnabled(Alias.second))
+ CheckNames.push_back(Alias.second);
+
for (const auto &AnalyzerCheck : getCheckersControlList(Context))
CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
@@ -492,6 +497,13 @@
if (Opts.ExtraArgs)
AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
Opts.ExtraArgs->end());
+
+ for (const auto &Alias : Context.WarningCheckAliases)
+ if (Context.isCheckEnabled(Alias.second))
+ AdjustedArgs.push_back(
+ "-W" +
+ DiagnosticIDs::getWarningOptionForDiag(Alias.first).str());
+
return AdjustedArgs;
};
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits