[tidy] Expose getID to tidy checks

This also fixes a possible use-after-free in the IdentifierNamingCheck.

Differential Revision: https://reviews.llvm.org/D150254
This commit is contained in:
Kadir Cetinkaya 2023-05-10 09:47:17 +02:00
parent 655f0fc4b9
commit 2b240cc377
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
3 changed files with 5 additions and 3 deletions

View File

@ -407,7 +407,6 @@ public:
private: private:
void run(const ast_matchers::MatchFinder::MatchResult &Result) override; void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
StringRef getID() const override { return CheckName; }
std::string CheckName; std::string CheckName;
ClangTidyContext *Context; ClangTidyContext *Context;
@ -422,6 +421,7 @@ protected:
bool areDiagsSelfContained() const { bool areDiagsSelfContained() const {
return Context->areDiagsSelfContained(); return Context->areDiagsSelfContained();
} }
StringRef getID() const override { return CheckName; }
}; };
/// Read a named option from the ``Context`` and parse it as a bool. /// Read a named option from the ``Context`` and parse it as a bool.

View File

@ -14,6 +14,7 @@
#include "clang/Lex/Preprocessor.h" #include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMapInfo.h" #include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h" #include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/FormatVariadic.h"
@ -396,7 +397,7 @@ std::string IdentifierNamingCheck::HungarianNotation::getDeclTypeName(
IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name, IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
ClangTidyContext *Context) ClangTidyContext *Context)
: RenamerClangTidyCheck(Name, Context), Context(Context), CheckName(Name), : RenamerClangTidyCheck(Name, Context), Context(Context),
GetConfigPerFile(Options.get("GetConfigPerFile", true)), GetConfigPerFile(Options.get("GetConfigPerFile", true)),
IgnoreFailedSplit(Options.get("IgnoreFailedSplit", false)) { IgnoreFailedSplit(Options.get("IgnoreFailedSplit", false)) {
@ -1461,6 +1462,7 @@ IdentifierNamingCheck::getStyleForFile(StringRef FileName) const {
if (Iter != NamingStylesCache.end()) if (Iter != NamingStylesCache.end())
return Iter->getValue(); return Iter->getValue();
llvm::StringRef CheckName = getID();
ClangTidyOptions Options = Context->getOptionsForFile(FileName); ClangTidyOptions Options = Context->getOptionsForFile(FileName);
if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) { if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) {
auto It = NamingStylesCache.try_emplace( auto It = NamingStylesCache.try_emplace(

View File

@ -11,6 +11,7 @@
#include "../utils/RenamerClangTidyCheck.h" #include "../utils/RenamerClangTidyCheck.h"
#include <optional> #include <optional>
#include <string>
namespace clang::tidy { namespace clang::tidy {
namespace readability { namespace readability {
@ -202,7 +203,6 @@ private:
mutable llvm::StringMap<FileStyle> NamingStylesCache; mutable llvm::StringMap<FileStyle> NamingStylesCache;
FileStyle *MainFileStyle; FileStyle *MainFileStyle;
ClangTidyContext *Context; ClangTidyContext *Context;
const StringRef CheckName;
const bool GetConfigPerFile; const bool GetConfigPerFile;
const bool IgnoreFailedSplit; const bool IgnoreFailedSplit;
HungarianNotation HungarianNotation; HungarianNotation HungarianNotation;