From 593e408fb63c3b49b8bec1d8b50b5c5ef87d39f7 Mon Sep 17 00:00:00 2001 From: Vlad Serebrennikov Date: Tue, 22 Oct 2024 22:21:08 +0400 Subject: [PATCH] [clang-tidy][NFC] Replace usages of `DeclSpec::TQ` with `Qualifiers::TQ` (#113295) This patch improves, but doens't fully resolve the layering violation, which stems from relying on Sema. There's one function that needs to convert enumerator to a string (`buildQualifier` in `FixItHintUtils.cpp`), but `Qualifiers::TQ` doesn't offer such function. Even more, the set of enumerators is not complete compared to `DeclSpec::TQ`, so I'm afraid that this would be a functional change. --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 10 +++++----- .../performance/ForRangeCopyCheck.cpp | 4 ++-- .../UnnecessaryCopyInitialization.cpp | 2 +- .../UnnecessaryValueParamCheck.cpp | 2 +- .../clang-tidy/utils/FixItHintUtils.cpp | 19 +++++++++++-------- .../clang-tidy/utils/FixItHintUtils.h | 4 ++-- .../unittests/clang-tidy/AddConstTest.cpp | 4 ++-- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index e20cf6fbcb55a75..71a4cee4bdc6ef6 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -172,8 +172,8 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { using namespace utils::fixit; if (VC == VariableCategory::Value && TransformValues) { - Diag << addQualifierToVarDecl(*Variable, *Result.Context, - DeclSpec::TQ_const, QualifierTarget::Value, + Diag << addQualifierToVarDecl(*Variable, *Result.Context, Qualifiers::Const, + QualifierTarget::Value, QualifierPolicy::Right); // FIXME: Add '{}' for default initialization if no user-defined default // constructor exists and there is no initializer. @@ -181,8 +181,8 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { } if (VC == VariableCategory::Reference && TransformReferences) { - Diag << addQualifierToVarDecl(*Variable, *Result.Context, - DeclSpec::TQ_const, QualifierTarget::Value, + Diag << addQualifierToVarDecl(*Variable, *Result.Context, Qualifiers::Const, + QualifierTarget::Value, QualifierPolicy::Right); return; } @@ -190,7 +190,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { if (VC == VariableCategory::Pointer) { if (WarnPointersAsValues && TransformPointersAsValues) { Diag << addQualifierToVarDecl(*Variable, *Result.Context, - DeclSpec::TQ_const, QualifierTarget::Value, + Qualifiers::Const, QualifierTarget::Value, QualifierPolicy::Right); } return; diff --git a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp index 655e480ffa62cbb..f7d44bf86314195 100644 --- a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp @@ -91,7 +91,7 @@ bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar, << utils::fixit::changeVarDeclToReference(LoopVar, Context); if (!LoopVar.getType().isConstQualified()) { if (std::optional Fix = utils::fixit::addQualifierToVarDecl( - LoopVar, Context, DeclSpec::TQ::TQ_const)) + LoopVar, Context, Qualifiers::Const)) Diagnostic << *Fix; } return true; @@ -129,7 +129,7 @@ bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( "making it a const reference"); if (std::optional Fix = utils::fixit::addQualifierToVarDecl( - LoopVar, Context, DeclSpec::TQ::TQ_const)) + LoopVar, Context, Qualifiers::Const)) Diag << *Fix << utils::fixit::changeVarDeclToReference(LoopVar, Context); return true; diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp index 61240fa4b0eb8e3..034894c11bf2c02 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp @@ -36,7 +36,7 @@ void recordFixes(const VarDecl &Var, ASTContext &Context, Diagnostic << utils::fixit::changeVarDeclToReference(Var, Context); if (!Var.getType().isLocalConstQualified()) { if (std::optional Fix = utils::fixit::addQualifierToVarDecl( - Var, Context, DeclSpec::TQ::TQ_const)) + Var, Context, Qualifiers::Const)) Diagnostic << *Fix; } } diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp index 3a255c5c133f1d1..d356f866a8804b8 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp @@ -172,7 +172,7 @@ void UnnecessaryValueParamCheck::handleConstRefFix(const FunctionDecl &Function, // declaration. if (!CurrentParam.getType().getCanonicalType().isConstQualified()) { if (std::optional Fix = utils::fixit::addQualifierToVarDecl( - CurrentParam, Context, DeclSpec::TQ::TQ_const)) + CurrentParam, Context, Qualifiers::Const)) Diag << *Fix; } } diff --git a/clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp b/clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp index bbdd4326b0bac20..a15589f9721c761 100644 --- a/clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/FixItHintUtils.cpp @@ -11,6 +11,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/ExprCXX.h" #include "clang/AST/Type.h" +#include "clang/Sema/DeclSpec.h" #include "clang/Tooling/FixIt.h" #include @@ -71,15 +72,17 @@ static std::optional fixIfNotDangerous(SourceLocation Loc, // Build a string that can be emitted as FixIt with either a space in before // or after the qualifier, either ' const' or 'const '. -static std::string buildQualifier(DeclSpec::TQ Qualifier, +static std::string buildQualifier(Qualifiers::TQ Qualifier, bool WhitespaceBefore = false) { if (WhitespaceBefore) - return (llvm::Twine(' ') + DeclSpec::getSpecifierName(Qualifier)).str(); - return (llvm::Twine(DeclSpec::getSpecifierName(Qualifier)) + " ").str(); + return (llvm::Twine(' ') + Qualifiers::fromCVRMask(Qualifier).getAsString()) + .str(); + return (llvm::Twine(Qualifiers::fromCVRMask(Qualifier).getAsString()) + " ") + .str(); } static std::optional changeValue(const VarDecl &Var, - DeclSpec::TQ Qualifier, + Qualifiers::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context) { @@ -99,7 +102,7 @@ static std::optional changeValue(const VarDecl &Var, } static std::optional changePointerItself(const VarDecl &Var, - DeclSpec::TQ Qualifier, + Qualifiers::TQ Qualifier, const ASTContext &Context) { if (locDangerous(Var.getLocation())) return std::nullopt; @@ -112,7 +115,7 @@ static std::optional changePointerItself(const VarDecl &Var, } static std::optional -changePointer(const VarDecl &Var, DeclSpec::TQ Qualifier, const Type *Pointee, +changePointer(const VarDecl &Var, Qualifiers::TQ Qualifier, const Type *Pointee, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context) { // The pointer itself shall be marked as `const`. This is always to the right @@ -163,7 +166,7 @@ changePointer(const VarDecl &Var, DeclSpec::TQ Qualifier, const Type *Pointee, } static std::optional -changeReferencee(const VarDecl &Var, DeclSpec::TQ Qualifier, QualType Pointee, +changeReferencee(const VarDecl &Var, Qualifiers::TQ Qualifier, QualType Pointee, QualifierTarget QualTarget, QualifierPolicy QualPolicy, const ASTContext &Context) { if (QualPolicy == QualifierPolicy::Left && isValueType(Pointee)) @@ -183,7 +186,7 @@ changeReferencee(const VarDecl &Var, DeclSpec::TQ Qualifier, QualType Pointee, std::optional addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, - DeclSpec::TQ Qualifier, + Qualifiers::TQ Qualifier, QualifierTarget QualTarget, QualifierPolicy QualPolicy) { assert((QualPolicy == QualifierPolicy::Left || diff --git a/clang-tools-extra/clang-tidy/utils/FixItHintUtils.h b/clang-tools-extra/clang-tidy/utils/FixItHintUtils.h index 2b96b2b2ce600ce..e690dbaefe64261 100644 --- a/clang-tools-extra/clang-tidy/utils/FixItHintUtils.h +++ b/clang-tools-extra/clang-tidy/utils/FixItHintUtils.h @@ -11,7 +11,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/Decl.h" -#include "clang/Sema/DeclSpec.h" +#include "clang/AST/Type.h" #include namespace clang::tidy::utils::fixit { @@ -41,7 +41,7 @@ enum class QualifierTarget { /// Requires that `Var` is isolated in written code like in `int foo = 42;`. std::optional addQualifierToVarDecl(const VarDecl &Var, const ASTContext &Context, - DeclSpec::TQ Qualifier, + Qualifiers::TQ Qualifier, QualifierTarget QualTarget = QualifierTarget::Pointee, QualifierPolicy QualPolicy = QualifierPolicy::Left); diff --git a/clang-tools-extra/unittests/clang-tidy/AddConstTest.cpp b/clang-tools-extra/unittests/clang-tidy/AddConstTest.cpp index dfae25f3f26eb6c..d8c76049e393f98 100644 --- a/clang-tools-extra/unittests/clang-tidy/AddConstTest.cpp +++ b/clang-tools-extra/unittests/clang-tidy/AddConstTest.cpp @@ -27,8 +27,8 @@ class ConstTransform : public ClangTidyCheck { void check(const MatchFinder::MatchResult &Result) override { const auto *D = Result.Nodes.getNodeAs("var"); using utils::fixit::addQualifierToVarDecl; - std::optional Fix = addQualifierToVarDecl( - *D, *Result.Context, DeclSpec::TQ::TQ_const, CT, CP); + std::optional Fix = + addQualifierToVarDecl(*D, *Result.Context, Qualifiers::Const, CT, CP); auto Diag = diag(D->getBeginLoc(), "doing const transformation"); if (Fix) Diag << *Fix;