diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 13:58:24 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 14:11:53 +0300 |
commit | 11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch) | |
tree | fabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/tools/extra/clang-tidy/misc | |
parent | 9685917341315774aad5733b1793b1e533a88bbb (diff) | |
download | ydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz |
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/tools/extra/clang-tidy/misc')
40 files changed, 4935 insertions, 0 deletions
diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp new file mode 100644 index 0000000000..355e097108 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.cpp @@ -0,0 +1,170 @@ +//===--- ConfusableIdentifierCheck.cpp - +// clang-tidy--------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ConfusableIdentifierCheck.h" + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/Preprocessor.h" +#include "llvm/Support/ConvertUTF.h" + +namespace { +// Preprocessed version of +// https://www.unicode.org/Public/security/latest/confusables.txt +// +// This contains a sorted array of { UTF32 codepoint; UTF32 values[N];} +#include "Confusables.inc" +} // namespace + +namespace clang::tidy::misc { + +ConfusableIdentifierCheck::ConfusableIdentifierCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + +ConfusableIdentifierCheck::~ConfusableIdentifierCheck() = default; + +// Build a skeleton out of the Original identifier, inspired by the algorithm +// described in http://www.unicode.org/reports/tr39/#def-skeleton +// +// FIXME: TR39 mandates: +// +// For an input string X, define skeleton(X) to be the following transformation +// on the string: +// +// 1. Convert X to NFD format, as described in [UAX15]. +// 2. Concatenate the prototypes for each character in X according to the +// specified data, producing a string of exemplar characters. +// 3. Reapply NFD. +// +// We're skipping 1. and 3. for the sake of simplicity, but this can lead to +// false positive. + +std::string ConfusableIdentifierCheck::skeleton(StringRef Name) { + using namespace llvm; + std::string SName = Name.str(); + std::string Skeleton; + Skeleton.reserve(1 + Name.size()); + + const char *Curr = SName.c_str(); + const char *End = Curr + SName.size(); + while (Curr < End) { + + const char *Prev = Curr; + UTF32 CodePoint; + ConversionResult Result = convertUTF8Sequence( + reinterpret_cast<const UTF8 **>(&Curr), + reinterpret_cast<const UTF8 *>(End), &CodePoint, strictConversion); + if (Result != conversionOK) { + errs() << "Unicode conversion issue\n"; + break; + } + + StringRef Key(Prev, Curr - Prev); + auto Where = llvm::lower_bound(ConfusableEntries, CodePoint, + [](decltype(ConfusableEntries[0]) x, + UTF32 y) { return x.codepoint < y; }); + if (Where == std::end(ConfusableEntries) || CodePoint != Where->codepoint) { + Skeleton.append(Prev, Curr); + } else { + UTF8 Buffer[32]; + UTF8 *BufferStart = std::begin(Buffer); + UTF8 *IBuffer = BufferStart; + const UTF32 *ValuesStart = std::begin(Where->values); + const UTF32 *ValuesEnd = llvm::find(Where->values, '\0'); + if (ConvertUTF32toUTF8(&ValuesStart, ValuesEnd, &IBuffer, + std::end(Buffer), + strictConversion) != conversionOK) { + errs() << "Unicode conversion issue\n"; + break; + } + Skeleton.append((char *)BufferStart, (char *)IBuffer); + } + } + return Skeleton; +} + +static bool mayShadowImpl(const NamedDecl *ND0, const NamedDecl *ND1) { + const DeclContext *DC0 = ND0->getDeclContext()->getPrimaryContext(); + const DeclContext *DC1 = ND1->getDeclContext()->getPrimaryContext(); + + if (isa<TemplateTypeParmDecl>(ND0) || isa<TemplateTypeParmDecl>(ND0)) + return true; + + while (DC0->isTransparentContext()) + DC0 = DC0->getParent(); + while (DC1->isTransparentContext()) + DC1 = DC1->getParent(); + + if (DC0->Equals(DC1)) + return true; + + return false; +} + +static bool isMemberOf(const NamedDecl *ND, const CXXRecordDecl *RD) { + const DeclContext *NDParent = ND->getDeclContext(); + if (!NDParent || !isa<CXXRecordDecl>(NDParent)) + return false; + if (NDParent == RD) + return true; + return !RD->forallBases( + [NDParent](const CXXRecordDecl *Base) { return NDParent != Base; }); +} + +static bool mayShadow(const NamedDecl *ND0, const NamedDecl *ND1) { + + const DeclContext *DC0 = ND0->getDeclContext()->getPrimaryContext(); + const DeclContext *DC1 = ND1->getDeclContext()->getPrimaryContext(); + + if (const CXXRecordDecl *RD0 = dyn_cast<CXXRecordDecl>(DC0)) { + RD0 = RD0->getDefinition(); + if (RD0 && ND1->getAccess() != AS_private && isMemberOf(ND1, RD0)) + return true; + } + if (const CXXRecordDecl *RD1 = dyn_cast<CXXRecordDecl>(DC1)) { + RD1 = RD1->getDefinition(); + if (RD1 && ND0->getAccess() != AS_private && isMemberOf(ND0, RD1)) + return true; + } + + if (DC0->Encloses(DC1)) + return mayShadowImpl(ND0, ND1); + if (DC1->Encloses(DC0)) + return mayShadowImpl(ND1, ND0); + return false; +} + +void ConfusableIdentifierCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + if (const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("nameddecl")) { + if (IdentifierInfo *NDII = ND->getIdentifier()) { + StringRef NDName = NDII->getName(); + llvm::SmallVector<const NamedDecl *> &Mapped = Mapper[skeleton(NDName)]; + for (const NamedDecl *OND : Mapped) { + const IdentifierInfo *ONDII = OND->getIdentifier(); + if (mayShadow(ND, OND)) { + StringRef ONDName = ONDII->getName(); + if (ONDName != NDName) { + diag(ND->getLocation(), "%0 is confusable with %1") << ND << OND; + diag(OND->getLocation(), "other declaration found here", + DiagnosticIDs::Note); + } + } + } + Mapped.push_back(ND); + } + } +} + +void ConfusableIdentifierCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + Finder->addMatcher(ast_matchers::namedDecl().bind("nameddecl"), this); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.h new file mode 100644 index 0000000000..b656696ef9 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConfusableIdentifierCheck.h @@ -0,0 +1,36 @@ +//===--- ConfusableIdentifierCheck.h - clang-tidy +//-------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONFUSABLE_IDENTIFIER_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONFUSABLE_IDENTIFIER_CHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// Finds symbol which have confusable identifiers, i.e. identifiers that look +/// the same visually but have a different Unicode representation. +/// If symbols are confusable but don't live in conflicting namespaces, they are +/// not reported. +class ConfusableIdentifierCheck : public ClangTidyCheck { +public: + ConfusableIdentifierCheck(StringRef Name, ClangTidyContext *Context); + ~ConfusableIdentifierCheck(); + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + std::string skeleton(StringRef); + llvm::StringMap<llvm::SmallVector<const NamedDecl *>> Mapper; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONFUSABLE_IDENTIFIER_CHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.cpp new file mode 100644 index 0000000000..b7c2f28d72 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -0,0 +1,208 @@ +//===--- ConstCorrectnessCheck.cpp - clang-tidy -----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ConstCorrectnessCheck.h" +#include "../utils/FixItHintUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { +// FIXME: This matcher exists in some other code-review as well. +// It should probably move to ASTMatchers. +AST_MATCHER(VarDecl, isLocal) { return Node.isLocalVarDecl(); } +AST_MATCHER_P(DeclStmt, containsAnyDeclaration, + ast_matchers::internal::Matcher<Decl>, InnerMatcher) { + return ast_matchers::internal::matchesFirstInPointerRange( + InnerMatcher, Node.decl_begin(), Node.decl_end(), Finder, + Builder) != Node.decl_end(); +} +AST_MATCHER(ReferenceType, isSpelledAsLValue) { + return Node.isSpelledAsLValue(); +} +AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); } +} // namespace + +ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + AnalyzeValues(Options.get("AnalyzeValues", true)), + AnalyzeReferences(Options.get("AnalyzeReferences", true)), + WarnPointersAsValues(Options.get("WarnPointersAsValues", false)), + TransformValues(Options.get("TransformValues", true)), + TransformReferences(Options.get("TransformReferences", true)), + TransformPointersAsValues( + Options.get("TransformPointersAsValues", false)) { + if (AnalyzeValues == false && AnalyzeReferences == false) + this->configurationDiag( + "The check 'misc-const-correctness' will not " + "perform any analysis because both 'AnalyzeValues' and " + "'AnalyzeReferences' are false."); +} + +void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "AnalyzeValues", AnalyzeValues); + Options.store(Opts, "AnalyzeReferences", AnalyzeReferences); + Options.store(Opts, "WarnPointersAsValues", WarnPointersAsValues); + + Options.store(Opts, "TransformValues", TransformValues); + Options.store(Opts, "TransformReferences", TransformReferences); + Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); +} + +void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { + const auto ConstType = hasType(isConstQualified()); + const auto ConstReference = hasType(references(isConstQualified())); + const auto RValueReference = hasType( + referenceType(anyOf(rValueReferenceType(), unless(isSpelledAsLValue())))); + + const auto TemplateType = anyOf( + hasType(hasCanonicalType(templateTypeParmType())), + hasType(substTemplateTypeParmType()), hasType(isDependentType()), + // References to template types, their substitutions or typedefs to + // template types need to be considered as well. + hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType())))), + hasType(referenceType(pointee(substTemplateTypeParmType())))); + + const auto AutoTemplateType = varDecl( + anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType()))), + hasType(pointerType(pointee(autoType()))))); + + const auto FunctionPointerRef = + hasType(hasCanonicalType(referenceType(pointee(functionType())))); + + // Match local variables which could be 'const' if not modified later. + // Example: `int i = 10` would match `int i`. + const auto LocalValDecl = varDecl( + allOf(isLocal(), hasInitializer(anything()), + unless(anyOf(ConstType, ConstReference, TemplateType, + hasInitializer(isInstantiationDependent()), + AutoTemplateType, RValueReference, FunctionPointerRef, + hasType(cxxRecordDecl(isLambda())), isImplicit())))); + + // Match the function scope for which the analysis of all local variables + // shall be run. + const auto FunctionScope = + functionDecl( + hasBody( + compoundStmt(forEachDescendant( + declStmt(containsAnyDeclaration( + LocalValDecl.bind("local-value")), + unless(has(decompositionDecl()))) + .bind("decl-stmt"))) + .bind("scope"))) + .bind("function-decl"); + + Finder->addMatcher(FunctionScope, this); +} + +/// Classify for a variable in what the Const-Check is interested. +enum class VariableCategory { Value, Reference, Pointer }; + +void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { + const auto *LocalScope = Result.Nodes.getNodeAs<CompoundStmt>("scope"); + const auto *Variable = Result.Nodes.getNodeAs<VarDecl>("local-value"); + const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function-decl"); + + /// If the variable was declared in a template it might be analyzed multiple + /// times. Only one of those instantiations shall emit a warning. NOTE: This + /// shall only deduplicate warnings for variables that are not instantiation + /// dependent. Variables like 'int x = 42;' in a template that can become + /// const emit multiple warnings otherwise. + bool IsNormalVariableInTemplate = Function->isTemplateInstantiation(); + if (IsNormalVariableInTemplate && + TemplateDiagnosticsCache.contains(Variable->getBeginLoc())) + return; + + VariableCategory VC = VariableCategory::Value; + if (Variable->getType()->isReferenceType()) + VC = VariableCategory::Reference; + if (Variable->getType()->isPointerType()) + VC = VariableCategory::Pointer; + if (Variable->getType()->isArrayType()) { + if (const auto *ArrayT = dyn_cast<ArrayType>(Variable->getType())) { + if (ArrayT->getElementType()->isPointerType()) + VC = VariableCategory::Pointer; + } + } + + // Each variable can only be in one category: Value, Pointer, Reference. + // Analysis can be controlled for every category. + if (VC == VariableCategory::Reference && !AnalyzeReferences) + return; + + if (VC == VariableCategory::Reference && + Variable->getType()->getPointeeType()->isPointerType() && + !WarnPointersAsValues) + return; + + if (VC == VariableCategory::Pointer && !WarnPointersAsValues) + return; + + if (VC == VariableCategory::Value && !AnalyzeValues) + return; + + // The scope is only registered if the analysis shall be run. + registerScope(LocalScope, Result.Context); + + // Offload const-analysis to utility function. + if (ScopesCache[LocalScope]->isMutated(Variable)) + return; + + auto Diag = diag(Variable->getBeginLoc(), + "variable %0 of type %1 can be declared 'const'") + << Variable << Variable->getType(); + if (IsNormalVariableInTemplate) + TemplateDiagnosticsCache.insert(Variable->getBeginLoc()); + + const auto *VarDeclStmt = Result.Nodes.getNodeAs<DeclStmt>("decl-stmt"); + + // It can not be guaranteed that the variable is declared isolated, therefore + // a transformation might effect the other variables as well and be incorrect. + if (VarDeclStmt == nullptr || !VarDeclStmt->isSingleDecl()) + return; + + using namespace utils::fixit; + if (VC == VariableCategory::Value && TransformValues) { + Diag << addQualifierToVarDecl(*Variable, *Result.Context, + DeclSpec::TQ_const, QualifierTarget::Value, + QualifierPolicy::Right); + // FIXME: Add '{}' for default initialization if no user-defined default + // constructor exists and there is no initializer. + return; + } + + if (VC == VariableCategory::Reference && TransformReferences) { + Diag << addQualifierToVarDecl(*Variable, *Result.Context, + DeclSpec::TQ_const, QualifierTarget::Value, + QualifierPolicy::Right); + return; + } + + if (VC == VariableCategory::Pointer) { + if (WarnPointersAsValues && TransformPointersAsValues) { + Diag << addQualifierToVarDecl(*Variable, *Result.Context, + DeclSpec::TQ_const, QualifierTarget::Value, + QualifierPolicy::Right); + } + return; + } +} + +void ConstCorrectnessCheck::registerScope(const CompoundStmt *LocalScope, + ASTContext *Context) { + auto &Analyzer = ScopesCache[LocalScope]; + if (!Analyzer) + Analyzer = std::make_unique<ExprMutationAnalyzer>(*LocalScope, *Context); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.h new file mode 100644 index 0000000000..08ffde5245 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -0,0 +1,52 @@ +//===--- ConstCorrectnessCheck.h - clang-tidy -------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONSTCORRECTNESSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONSTCORRECTNESSCHECK_H + +#include "../ClangTidyCheck.h" +#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" +#include "llvm/ADT/DenseSet.h" + +namespace clang::tidy::misc { + +/// This check warns on variables which could be declared const but are not. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/const-correctness.html +class ConstCorrectnessCheck : public ClangTidyCheck { +public: + ConstCorrectnessCheck(StringRef Name, ClangTidyContext *Context); + + // The rules for C and 'const' are different and incompatible for this check. + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + void registerScope(const CompoundStmt *LocalScope, ASTContext *Context); + + using MutationAnalyzer = std::unique_ptr<ExprMutationAnalyzer>; + llvm::DenseMap<const CompoundStmt *, MutationAnalyzer> ScopesCache; + llvm::DenseSet<SourceLocation> TemplateDiagnosticsCache; + + const bool AnalyzeValues; + const bool AnalyzeReferences; + const bool WarnPointersAsValues; + + const bool TransformValues; + const bool TransformReferences; + const bool TransformPointersAsValues; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_CONSTCORRECTNESSCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp new file mode 100644 index 0000000000..c894d7edbf --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp @@ -0,0 +1,161 @@ +//===--- DefinitionsInHeadersCheck.cpp - clang-tidy------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "DefinitionsInHeadersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +AST_MATCHER_P(NamedDecl, usesHeaderFileExtension, utils::FileExtensionsSet, + HeaderFileExtensions) { + return utils::isExpansionLocInHeaderFile( + Node.getBeginLoc(), Finder->getASTContext().getSourceManager(), + HeaderFileExtensions); +} + +} // namespace + +DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + UseHeaderFileExtension(Options.get("UseHeaderFileExtension", true)), + RawStringHeaderFileExtensions(Options.getLocalOrGlobal( + "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, + utils::defaultFileExtensionDelimiters())) { + this->configurationDiag("Invalid header file extension: '%0'") + << RawStringHeaderFileExtensions; + } +} + +void DefinitionsInHeadersCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "UseHeaderFileExtension", UseHeaderFileExtension); + Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); +} + +void DefinitionsInHeadersCheck::registerMatchers(MatchFinder *Finder) { + auto DefinitionMatcher = + anyOf(functionDecl(isDefinition(), unless(isDeleted())), + varDecl(isDefinition())); + if (UseHeaderFileExtension) { + Finder->addMatcher(namedDecl(DefinitionMatcher, + usesHeaderFileExtension(HeaderFileExtensions)) + .bind("name-decl"), + this); + } else { + Finder->addMatcher( + namedDecl(DefinitionMatcher, + anyOf(usesHeaderFileExtension(HeaderFileExtensions), + unless(isExpansionInMainFile()))) + .bind("name-decl"), + this); + } +} + +void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) { + // Don't run the check in failing TUs. + if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred()) + return; + + // C++ [basic.def.odr] p6: + // There can be more than one definition of a class type, enumeration type, + // inline function with external linkage, class template, non-static function + // template, static data member of a class template, member function of a + // class template, or template specialization for which some template + // parameters are not specifiedin a program provided that each definition + // appears in a different translation unit, and provided the definitions + // satisfy the following requirements. + const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("name-decl"); + assert(ND); + if (ND->isInvalidDecl()) + return; + + // Internal linkage variable definitions are ignored for now: + // const int a = 1; + // static int b = 1; + // + // Although these might also cause ODR violations, we can be less certain and + // should try to keep the false-positive rate down. + // + // FIXME: Should declarations in anonymous namespaces get the same treatment + // as static / const declarations? + if (!ND->hasExternalFormalLinkage() && !ND->isInAnonymousNamespace()) + return; + + if (const auto *FD = dyn_cast<FunctionDecl>(ND)) { + // Inline functions are allowed. + if (FD->isInlined()) + return; + // Function templates are allowed. + if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) + return; + // Ignore instantiated functions. + if (FD->isTemplateInstantiation()) + return; + // Member function of a class template and member function of a nested class + // in a class template are allowed. + if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { + const auto *DC = MD->getDeclContext(); + while (DC->isRecord()) { + if (const auto *RD = dyn_cast<CXXRecordDecl>(DC)) { + if (isa<ClassTemplatePartialSpecializationDecl>(RD)) + return; + if (RD->getDescribedClassTemplate()) + return; + } + DC = DC->getParent(); + } + } + + bool IsFullSpec = FD->getTemplateSpecializationKind() != TSK_Undeclared; + diag(FD->getLocation(), + "%select{function|full function template specialization}0 %1 defined " + "in a header file; function definitions in header files can lead to " + "ODR violations") + << IsFullSpec << FD; + // inline is not allowed for main function. + if (FD->isMain()) + return; + diag(FD->getLocation(), /*Description=*/"make as 'inline'", + DiagnosticIDs::Note) + << FixItHint::CreateInsertion(FD->getInnerLocStart(), "inline "); + } else if (const auto *VD = dyn_cast<VarDecl>(ND)) { + // C++14 variable templates are allowed. + if (VD->getDescribedVarTemplate()) + return; + // Static data members of a class template are allowed. + if (VD->getDeclContext()->isDependentContext() && VD->isStaticDataMember()) + return; + // Ignore instantiated static data members of classes. + if (isTemplateInstantiation(VD->getTemplateSpecializationKind())) + return; + // Ignore variable definition within function scope. + if (VD->hasLocalStorage() || VD->isStaticLocal()) + return; + // Ignore inline variables. + if (VD->isInline()) + return; + // Ignore partial specializations. + if (isa<VarTemplatePartialSpecializationDecl>(VD)) + return; + + diag(VD->getLocation(), + "variable %0 defined in a header file; " + "variable definitions in header files can lead to ODR violations") + << VD; + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.h new file mode 100644 index 0000000000..db32cabc95 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/DefinitionsInHeadersCheck.h @@ -0,0 +1,50 @@ +//===--- DefinitionsInHeadersCheck.h - clang-tidy----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DEFINITIONS_IN_HEADERS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DEFINITIONS_IN_HEADERS_H + +#include "../ClangTidyCheck.h" +#include "../utils/FileExtensionsUtils.h" + +namespace clang::tidy::misc { + +/// Finds non-extern non-inline function and variable definitions in header +/// files, which can lead to potential ODR violations. +/// +/// The check supports these options: +/// - `UseHeaderFileExtension`: Whether to use file extension to distinguish +/// header files. True by default. +/// - `HeaderFileExtensions`: a semicolon-separated list of filename +/// extensions of header files (The filename extension should not contain +/// "." prefix). ";h;hh;hpp;hxx" by default. +/// +/// For extension-less header files, using an empty string or leaving an +/// empty string between ";" if there are other filename extensions. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/definitions-in-headers.html +class DefinitionsInHeadersCheck : public ClangTidyCheck { +public: + DefinitionsInHeadersCheck(StringRef Name, ClangTidyContext *Context); + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus11; + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool UseHeaderFileExtension; + const StringRef RawStringHeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DEFINITIONS_IN_HEADERS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MiscTidyModule.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MiscTidyModule.cpp new file mode 100644 index 0000000000..2ec61f8912 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MiscTidyModule.cpp @@ -0,0 +1,86 @@ +//===--- MiscTidyModule.cpp - clang-tidy ----------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "../ClangTidy.h" +#include "../ClangTidyModule.h" +#include "../ClangTidyModuleRegistry.h" +#include "ConfusableIdentifierCheck.h" +#include "ConstCorrectnessCheck.h" +#include "DefinitionsInHeadersCheck.h" +#include "MisleadingBidirectional.h" +#include "MisleadingIdentifier.h" +#include "MisplacedConstCheck.h" +#include "NewDeleteOverloadsCheck.h" +#include "NoRecursionCheck.h" +#include "NonCopyableObjects.h" +#include "NonPrivateMemberVariablesInClassesCheck.h" +#include "RedundantExpressionCheck.h" +#include "StaticAssertCheck.h" +#include "ThrowByValueCatchByReferenceCheck.h" +#include "UnconventionalAssignOperatorCheck.h" +#include "UniqueptrResetReleaseCheck.h" +#include "UnusedAliasDeclsCheck.h" +#include "UnusedParametersCheck.h" +#include "UnusedUsingDeclsCheck.h" +#include "UseAnonymousNamespaceCheck.h" + +namespace clang::tidy { +namespace misc { + +class MiscModule : public ClangTidyModule { +public: + void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { + CheckFactories.registerCheck<ConfusableIdentifierCheck>( + "misc-confusable-identifiers"); + CheckFactories.registerCheck<ConstCorrectnessCheck>( + "misc-const-correctness"); + CheckFactories.registerCheck<DefinitionsInHeadersCheck>( + "misc-definitions-in-headers"); + CheckFactories.registerCheck<MisleadingBidirectionalCheck>( + "misc-misleading-bidirectional"); + CheckFactories.registerCheck<MisleadingIdentifierCheck>( + "misc-misleading-identifier"); + CheckFactories.registerCheck<MisplacedConstCheck>("misc-misplaced-const"); + CheckFactories.registerCheck<NewDeleteOverloadsCheck>( + "misc-new-delete-overloads"); + CheckFactories.registerCheck<NoRecursionCheck>("misc-no-recursion"); + CheckFactories.registerCheck<NonCopyableObjectsCheck>( + "misc-non-copyable-objects"); + CheckFactories.registerCheck<NonPrivateMemberVariablesInClassesCheck>( + "misc-non-private-member-variables-in-classes"); + CheckFactories.registerCheck<RedundantExpressionCheck>( + "misc-redundant-expression"); + CheckFactories.registerCheck<StaticAssertCheck>("misc-static-assert"); + CheckFactories.registerCheck<ThrowByValueCatchByReferenceCheck>( + "misc-throw-by-value-catch-by-reference"); + CheckFactories.registerCheck<UnconventionalAssignOperatorCheck>( + "misc-unconventional-assign-operator"); + CheckFactories.registerCheck<UniqueptrResetReleaseCheck>( + "misc-uniqueptr-reset-release"); + CheckFactories.registerCheck<UnusedAliasDeclsCheck>( + "misc-unused-alias-decls"); + CheckFactories.registerCheck<UnusedParametersCheck>( + "misc-unused-parameters"); + CheckFactories.registerCheck<UnusedUsingDeclsCheck>( + "misc-unused-using-decls"); + CheckFactories.registerCheck<UseAnonymousNamespaceCheck>( + "misc-use-anonymous-namespace"); + } +}; + +} // namespace misc + +// Register the MiscTidyModule using this statically initialized variable. +static ClangTidyModuleRegistry::Add<misc::MiscModule> + X("misc-module", "Adds miscellaneous lint checks."); + +// This anchor is used to force the linker to link in the generated object file +// and thus register the MiscModule. +volatile int MiscModuleAnchorSource = 0; + +} // namespace clang::tidy diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.cpp new file mode 100644 index 0000000000..dad6737ab4 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.cpp @@ -0,0 +1,138 @@ +//===--- MisleadingBidirectional.cpp - clang-tidy -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "MisleadingBidirectional.h" + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/Preprocessor.h" +#include "llvm/Support/ConvertUTF.h" +#include <optional> + +using namespace clang; +using namespace clang::tidy::misc; + +static bool containsMisleadingBidi(StringRef Buffer, + bool HonorLineBreaks = true) { + const char *CurPtr = Buffer.begin(); + + enum BidiChar { + PS = 0x2029, + RLO = 0x202E, + RLE = 0x202B, + LRO = 0x202D, + LRE = 0x202A, + PDF = 0x202C, + RLI = 0x2067, + LRI = 0x2066, + FSI = 0x2068, + PDI = 0x2069 + }; + + SmallVector<BidiChar> BidiContexts; + + // Scan each character while maintaining a stack of opened bidi context. + // RLO/RLE/LRO/LRE all are closed by PDF while RLI LRI and FSI are closed by + // PDI. New lines reset the context count. Extra PDF / PDI are ignored. + // + // Warn if we end up with an unclosed context. + while (CurPtr < Buffer.end()) { + unsigned char C = *CurPtr; + if (isASCII(C)) { + ++CurPtr; + bool IsParagrapSep = + (C == 0xA || C == 0xD || (0x1C <= C && C <= 0x1E) || C == 0x85); + bool IsSegmentSep = (C == 0x9 || C == 0xB || C == 0x1F); + if (IsParagrapSep || IsSegmentSep) + BidiContexts.clear(); + continue; + } + llvm::UTF32 CodePoint; + llvm::ConversionResult Result = llvm::convertUTF8Sequence( + (const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)Buffer.end(), + &CodePoint, llvm::strictConversion); + + // If conversion fails, utf-8 is designed so that we can just try next char. + if (Result != llvm::conversionOK) { + ++CurPtr; + continue; + } + + // Open a PDF context. + if (CodePoint == RLO || CodePoint == RLE || CodePoint == LRO || + CodePoint == LRE) + BidiContexts.push_back(PDF); + // Close PDF Context. + else if (CodePoint == PDF) { + if (!BidiContexts.empty() && BidiContexts.back() == PDF) + BidiContexts.pop_back(); + } + // Open a PDI Context. + else if (CodePoint == RLI || CodePoint == LRI || CodePoint == FSI) + BidiContexts.push_back(PDI); + // Close a PDI Context. + else if (CodePoint == PDI) { + auto R = llvm::find(llvm::reverse(BidiContexts), PDI); + if (R != BidiContexts.rend()) + BidiContexts.resize(BidiContexts.rend() - R - 1); + } + // Line break or equivalent + else if (CodePoint == PS) + BidiContexts.clear(); + } + return !BidiContexts.empty(); +} + +class MisleadingBidirectionalCheck::MisleadingBidirectionalHandler + : public CommentHandler { +public: + MisleadingBidirectionalHandler(MisleadingBidirectionalCheck &Check) + : Check(Check) {} + + bool HandleComment(Preprocessor &PP, SourceRange Range) override { + // FIXME: check that we are in a /* */ comment + StringRef Text = + Lexer::getSourceText(CharSourceRange::getCharRange(Range), + PP.getSourceManager(), PP.getLangOpts()); + + if (containsMisleadingBidi(Text, true)) + Check.diag( + Range.getBegin(), + "comment contains misleading bidirectional Unicode characters"); + return false; + } + +private: + MisleadingBidirectionalCheck &Check; +}; + +MisleadingBidirectionalCheck::MisleadingBidirectionalCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + Handler(std::make_unique<MisleadingBidirectionalHandler>(*this)) {} + +MisleadingBidirectionalCheck::~MisleadingBidirectionalCheck() = default; + +void MisleadingBidirectionalCheck::registerPPCallbacks( + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { + PP->addCommentHandler(Handler.get()); +} + +void MisleadingBidirectionalCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + if (const auto *SL = Result.Nodes.getNodeAs<StringLiteral>("strlit")) { + StringRef Literal = SL->getBytes(); + if (containsMisleadingBidi(Literal, false)) + diag(SL->getBeginLoc(), "string literal contains misleading " + "bidirectional Unicode characters"); + } +} + +void MisleadingBidirectionalCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + Finder->addMatcher(ast_matchers::stringLiteral().bind("strlit"), this); +} diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.h new file mode 100644 index 0000000000..9ffb238aee --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingBidirectional.h @@ -0,0 +1,34 @@ +//===--- MisleadingBidirectionalCheck.h - clang-tidy ------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGBIDIRECTIONALCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGBIDIRECTIONALCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +class MisleadingBidirectionalCheck : public ClangTidyCheck { +public: + MisleadingBidirectionalCheck(StringRef Name, ClangTidyContext *Context); + ~MisleadingBidirectionalCheck(); + + void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, + Preprocessor *ModuleExpanderPP) override; + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + class MisleadingBidirectionalHandler; + std::unique_ptr<MisleadingBidirectionalHandler> Handler; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGBIDIRECTIONALCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.cpp new file mode 100644 index 0000000000..e1b98b86ec --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.cpp @@ -0,0 +1,162 @@ +//===--- MisleadingIdentifier.cpp - clang-tidy-----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "MisleadingIdentifier.h" + +#include "clang/Frontend/CompilerInstance.h" +#include "llvm/Support/ConvertUTF.h" + +namespace clang::tidy::misc { + +// See https://www.unicode.org/Public/14.0.0/ucd/extracted/DerivedBidiClass.txt +static bool isUnassignedAL(llvm::UTF32 CP) { + return (0x0600 <= CP && CP <= 0x07BF) || (0x0860 <= CP && CP <= 0x08FF) || + (0xFB50 <= CP && CP <= 0xFDCF) || (0xFDF0 <= CP && CP <= 0xFDFF) || + (0xFE70 <= CP && CP <= 0xFEFF) || + (0x00010D00 <= CP && CP <= 0x00010D3F) || + (0x00010F30 <= CP && CP <= 0x00010F6F) || + (0x0001EC70 <= CP && CP <= 0x0001ECBF) || + (0x0001ED00 <= CP && CP <= 0x0001ED4F) || + (0x0001EE00 <= CP && CP <= 0x0001EEFF); +} + +// See https://www.unicode.org/Public/14.0.0/ucd/extracted/DerivedBidiClass.txt +static bool isUnassignedR(llvm::UTF32 CP) { + return (0x0590 <= CP && CP <= 0x05FF) || (0x07C0 <= CP && CP <= 0x085F) || + (0xFB1D <= CP && CP <= 0xFB4F) || + (0x00010800 <= CP && CP <= 0x00010CFF) || + (0x00010D40 <= CP && CP <= 0x00010F2F) || + (0x00010F70 <= CP && CP <= 0x00010FFF) || + (0x0001E800 <= CP && CP <= 0x0001EC6F) || + (0x0001ECC0 <= CP && CP <= 0x0001ECFF) || + (0x0001ED50 <= CP && CP <= 0x0001EDFF) || + (0x0001EF00 <= CP && CP <= 0x0001EFFF); +} + +// See https://www.unicode.org/Public/14.0.0/ucd/extracted/DerivedBidiClass.txt +static bool isR(llvm::UTF32 CP) { + return (CP == 0x0590) || (CP == 0x05BE) || (CP == 0x05C0) || (CP == 0x05C3) || + (CP == 0x05C6) || (0x05C8 <= CP && CP <= 0x05CF) || + (0x05D0 <= CP && CP <= 0x05EA) || (0x05EB <= CP && CP <= 0x05EE) || + (0x05EF <= CP && CP <= 0x05F2) || (0x05F3 <= CP && CP <= 0x05F4) || + (0x05F5 <= CP && CP <= 0x05FF) || (0x07C0 <= CP && CP <= 0x07C9) || + (0x07CA <= CP && CP <= 0x07EA) || (0x07F4 <= CP && CP <= 0x07F5) || + (CP == 0x07FA) || (0x07FB <= CP && CP <= 0x07FC) || + (0x07FE <= CP && CP <= 0x07FF) || (0x0800 <= CP && CP <= 0x0815) || + (CP == 0x081A) || (CP == 0x0824) || (CP == 0x0828) || + (0x082E <= CP && CP <= 0x082F) || (0x0830 <= CP && CP <= 0x083E) || + (CP == 0x083F) || (0x0840 <= CP && CP <= 0x0858) || + (0x085C <= CP && CP <= 0x085D) || (CP == 0x085E) || (CP == 0x085F) || + (CP == 0x200F) || (CP == 0xFB1D) || (0xFB1F <= CP && CP <= 0xFB28) || + (0xFB2A <= CP && CP <= 0xFB36) || (CP == 0xFB37) || + (0xFB38 <= CP && CP <= 0xFB3C) || (CP == 0xFB3D) || (CP == 0xFB3E) || + (CP == 0xFB3F) || (0xFB40 <= CP && CP <= 0xFB41) || (CP == 0xFB42) || + (0xFB43 <= CP && CP <= 0xFB44) || (CP == 0xFB45) || + (0xFB46 <= CP && CP <= 0xFB4F) || (0x10800 <= CP && CP <= 0x10805) || + (0x10806 <= CP && CP <= 0x10807) || (CP == 0x10808) || + (CP == 0x10809) || (0x1080A <= CP && CP <= 0x10835) || + (CP == 0x10836) || (0x10837 <= CP && CP <= 0x10838) || + (0x10839 <= CP && CP <= 0x1083B) || (CP == 0x1083C) || + (0x1083D <= CP && CP <= 0x1083E) || (0x1083F <= CP && CP <= 0x10855) || + (CP == 0x10856) || (CP == 0x10857) || + (0x10858 <= CP && CP <= 0x1085F) || (0x10860 <= CP && CP <= 0x10876) || + (0x10877 <= CP && CP <= 0x10878) || (0x10879 <= CP && CP <= 0x1087F) || + (0x10880 <= CP && CP <= 0x1089E) || (0x1089F <= CP && CP <= 0x108A6) || + (0x108A7 <= CP && CP <= 0x108AF) || (0x108B0 <= CP && CP <= 0x108DF) || + (0x108E0 <= CP && CP <= 0x108F2) || (CP == 0x108F3) || + (0x108F4 <= CP && CP <= 0x108F5) || (0x108F6 <= CP && CP <= 0x108FA) || + (0x108FB <= CP && CP <= 0x108FF) || (0x10900 <= CP && CP <= 0x10915) || + (0x10916 <= CP && CP <= 0x1091B) || (0x1091C <= CP && CP <= 0x1091E) || + (0x10920 <= CP && CP <= 0x10939) || (0x1093A <= CP && CP <= 0x1093E) || + (CP == 0x1093F) || (0x10940 <= CP && CP <= 0x1097F) || + (0x10980 <= CP && CP <= 0x109B7) || (0x109B8 <= CP && CP <= 0x109BB) || + (0x109BC <= CP && CP <= 0x109BD) || (0x109BE <= CP && CP <= 0x109BF) || + (0x109C0 <= CP && CP <= 0x109CF) || (0x109D0 <= CP && CP <= 0x109D1) || + (0x109D2 <= CP && CP <= 0x109FF) || (CP == 0x10A00) || + (CP == 0x10A04) || (0x10A07 <= CP && CP <= 0x10A0B) || + (0x10A10 <= CP && CP <= 0x10A13) || (CP == 0x10A14) || + (0x10A15 <= CP && CP <= 0x10A17) || (CP == 0x10A18) || + (0x10A19 <= CP && CP <= 0x10A35) || (0x10A36 <= CP && CP <= 0x10A37) || + (0x10A3B <= CP && CP <= 0x10A3E) || (0x10A40 <= CP && CP <= 0x10A48) || + (0x10A49 <= CP && CP <= 0x10A4F) || (0x10A50 <= CP && CP <= 0x10A58) || + (0x10A59 <= CP && CP <= 0x10A5F) || (0x10A60 <= CP && CP <= 0x10A7C) || + (0x10A7D <= CP && CP <= 0x10A7E) || (CP == 0x10A7F) || + (0x10A80 <= CP && CP <= 0x10A9C) || (0x10A9D <= CP && CP <= 0x10A9F) || + (0x10AA0 <= CP && CP <= 0x10ABF) || (0x10AC0 <= CP && CP <= 0x10AC7) || + (CP == 0x10AC8) || (0x10AC9 <= CP && CP <= 0x10AE4) || + (0x10AE7 <= CP && CP <= 0x10AEA) || (0x10AEB <= CP && CP <= 0x10AEF) || + (0x10AF0 <= CP && CP <= 0x10AF6) || (0x10AF7 <= CP && CP <= 0x10AFF) || + (0x10B00 <= CP && CP <= 0x10B35) || (0x10B36 <= CP && CP <= 0x10B38) || + (0x10B40 <= CP && CP <= 0x10B55) || (0x10B56 <= CP && CP <= 0x10B57) || + (0x10B58 <= CP && CP <= 0x10B5F) || (0x10B60 <= CP && CP <= 0x10B72) || + (0x10B73 <= CP && CP <= 0x10B77) || (0x10B78 <= CP && CP <= 0x10B7F) || + (0x10B80 <= CP && CP <= 0x10B91) || (0x10B92 <= CP && CP <= 0x10B98) || + (0x10B99 <= CP && CP <= 0x10B9C) || (0x10B9D <= CP && CP <= 0x10BA8) || + (0x10BA9 <= CP && CP <= 0x10BAF) || (0x10BB0 <= CP && CP <= 0x10BFF) || + (0x10C00 <= CP && CP <= 0x10C48) || (0x10C49 <= CP && CP <= 0x10C7F) || + (0x10C80 <= CP && CP <= 0x10CB2) || (0x10CB3 <= CP && CP <= 0x10CBF) || + (0x10CC0 <= CP && CP <= 0x10CF2) || (0x10CF3 <= CP && CP <= 0x10CF9) || + (0x10CFA <= CP && CP <= 0x10CFF) || (0x10D40 <= CP && CP <= 0x10E5F) || + (CP == 0x10E7F) || (0x10E80 <= CP && CP <= 0x10EA9) || + (CP == 0x10EAA) || (CP == 0x10EAD) || + (0x10EAE <= CP && CP <= 0x10EAF) || (0x10EB0 <= CP && CP <= 0x10EB1) || + (0x10EB2 <= CP && CP <= 0x10EFF) || (0x10F00 <= CP && CP <= 0x10F1C) || + (0x10F1D <= CP && CP <= 0x10F26) || (CP == 0x10F27) || + (0x10F28 <= CP && CP <= 0x10F2F) || (0x10F70 <= CP && CP <= 0x10F81) || + (0x10F86 <= CP && CP <= 0x10F89) || (0x10F8A <= CP && CP <= 0x10FAF) || + (0x10FB0 <= CP && CP <= 0x10FC4) || (0x10FC5 <= CP && CP <= 0x10FCB) || + (0x10FCC <= CP && CP <= 0x10FDF) || (0x10FE0 <= CP && CP <= 0x10FF6) || + (0x10FF7 <= CP && CP <= 0x10FFF) || (0x1E800 <= CP && CP <= 0x1E8C4) || + (0x1E8C5 <= CP && CP <= 0x1E8C6) || (0x1E8C7 <= CP && CP <= 0x1E8CF) || + (0x1E8D7 <= CP && CP <= 0x1E8FF) || (0x1E900 <= CP && CP <= 0x1E943) || + (CP == 0x1E94B) || (0x1E94C <= CP && CP <= 0x1E94F) || + (0x1E950 <= CP && CP <= 0x1E959) || (0x1E95A <= CP && CP <= 0x1E95D) || + (0x1E95E <= CP && CP <= 0x1E95F) || (0x1E960 <= CP && CP <= 0x1EC6F) || + (0x1ECC0 <= CP && CP <= 0x1ECFF) || (0x1ED50 <= CP && CP <= 0x1EDFF); +} + +static bool hasRTLCharacters(StringRef Buffer) { + const char *CurPtr = Buffer.begin(); + const char *EndPtr = Buffer.end(); + while (CurPtr < EndPtr) { + llvm::UTF32 CodePoint; + llvm::ConversionResult Result = llvm::convertUTF8Sequence( + (const llvm::UTF8 **)&CurPtr, (const llvm::UTF8 *)EndPtr, &CodePoint, + llvm::strictConversion); + if (Result != llvm::conversionOK) + break; + if (isUnassignedAL(CodePoint) || isUnassignedR(CodePoint) || isR(CodePoint)) + return true; + } + return false; +} + +MisleadingIdentifierCheck::MisleadingIdentifierCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + +MisleadingIdentifierCheck::~MisleadingIdentifierCheck() = default; + +void MisleadingIdentifierCheck::check( + const ast_matchers::MatchFinder::MatchResult &Result) { + if (const auto *ND = Result.Nodes.getNodeAs<NamedDecl>("nameddecl")) { + IdentifierInfo *II = ND->getIdentifier(); + if (II) { + StringRef NDName = II->getName(); + if (hasRTLCharacters(NDName)) + diag(ND->getBeginLoc(), "identifier has right-to-left codepoints"); + } + } +} + +void MisleadingIdentifierCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + Finder->addMatcher(ast_matchers::namedDecl().bind("nameddecl"), this); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.h new file mode 100644 index 0000000000..7278b74124 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisleadingIdentifier.h @@ -0,0 +1,27 @@ +//===--- MisleadingIdentifierCheck.h - clang-tidy ---------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGIDENTIFIERCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGIDENTIFIERCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +class MisleadingIdentifierCheck : public ClangTidyCheck { +public: + MisleadingIdentifierCheck(StringRef Name, ClangTidyContext *Context); + ~MisleadingIdentifierCheck(); + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISLEADINGIDENTIFIERCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.cpp new file mode 100644 index 0000000000..4aaf64c9c5 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.cpp @@ -0,0 +1,75 @@ +//===--- MisplacedConstCheck.cpp - clang-tidy------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "MisplacedConstCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void MisplacedConstCheck::registerMatchers(MatchFinder *Finder) { + auto NonConstAndNonFunctionPointerType = hasType(pointerType(unless( + pointee(anyOf(isConstQualified(), ignoringParens(functionType())))))); + + Finder->addMatcher( + valueDecl(hasType(qualType( + isConstQualified(), + elaboratedType(namesType(typedefType(hasDeclaration( + anyOf(typedefDecl(NonConstAndNonFunctionPointerType) + .bind("typedef"), + typeAliasDecl(NonConstAndNonFunctionPointerType) + .bind("typeAlias"))))))))) + .bind("decl"), + this); +} + +static QualType guessAlternateQualification(ASTContext &Context, QualType QT) { + // We're given a QualType from a typedef where the qualifiers apply to the + // pointer instead of the pointee. Strip the const qualifier from the pointer + // type and add it to the pointee instead. + if (!QT->isPointerType()) + return QT; + + Qualifiers Quals = QT.getLocalQualifiers(); + Quals.removeConst(); + + QualType NewQT = Context.getPointerType( + QualType(QT->getPointeeType().getTypePtr(), Qualifiers::Const)); + return NewQT.withCVRQualifiers(Quals.getCVRQualifiers()); +} + +void MisplacedConstCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Var = Result.Nodes.getNodeAs<ValueDecl>("decl"); + ASTContext &Ctx = *Result.Context; + QualType CanQT = Var->getType().getCanonicalType(); + + SourceLocation AliasLoc; + const char *AliasType; + if (const auto *Typedef = Result.Nodes.getNodeAs<TypedefDecl>("typedef")) { + AliasLoc = Typedef->getLocation(); + AliasType = "typedef"; + } else if (const auto *TypeAlias = + Result.Nodes.getNodeAs<TypeAliasDecl>("typeAlias")) { + AliasLoc = TypeAlias->getLocation(); + AliasType = "type alias"; + } else { + llvm_unreachable("registerMatchers has registered an unknown matcher," + " code out of sync"); + } + + diag(Var->getLocation(), "%0 declared with a const-qualified %1; " + "results in the type being '%2' instead of '%3'") + << Var << AliasType << CanQT.getAsString(Ctx.getPrintingPolicy()) + << guessAlternateQualification(Ctx, CanQT) + .getAsString(Ctx.getPrintingPolicy()); + diag(AliasLoc, "%0 declared here", DiagnosticIDs::Note) << AliasType; +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.h new file mode 100644 index 0000000000..b2d88d41b5 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/MisplacedConstCheck.h @@ -0,0 +1,31 @@ +//===--- MisplacedConstCheck.h - clang-tidy----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISPLACED_CONST_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISPLACED_CONST_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// This check diagnoses when a const qualifier is applied to a typedef to a +/// pointer type rather than to the pointee. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/misplaced-const.html +class MisplacedConstCheck : public ClangTidyCheck { +public: + MisplacedConstCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MISPLACED_CONST_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.cpp new file mode 100644 index 0000000000..e772bcc9ed --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.cpp @@ -0,0 +1,205 @@ +//===--- NewDeleteOverloadsCheck.cpp - clang-tidy--------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NewDeleteOverloadsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +AST_MATCHER(FunctionDecl, isPlacementOverload) { + bool New; + switch (Node.getOverloadedOperator()) { + default: + return false; + case OO_New: + case OO_Array_New: + New = true; + break; + case OO_Delete: + case OO_Array_Delete: + New = false; + break; + } + + // Variadic functions are always placement functions. + if (Node.isVariadic()) + return true; + + // Placement new is easy: it always has more than one parameter (the first + // parameter is always the size). If it's an overload of delete or delete[] + // that has only one parameter, it's never a placement delete. + if (New) + return Node.getNumParams() > 1; + if (Node.getNumParams() == 1) + return false; + + // Placement delete is a little more challenging. They always have more than + // one parameter with the first parameter being a pointer. However, the + // second parameter can be a size_t for sized deallocation, and that is never + // a placement delete operator. + if (Node.getNumParams() <= 1 || Node.getNumParams() > 2) + return true; + + const auto *FPT = Node.getType()->castAs<FunctionProtoType>(); + ASTContext &Ctx = Node.getASTContext(); + if (Ctx.getLangOpts().SizedDeallocation && + Ctx.hasSameType(FPT->getParamType(1), Ctx.getSizeType())) + return false; + + return true; +} + +OverloadedOperatorKind getCorrespondingOverload(const FunctionDecl *FD) { + switch (FD->getOverloadedOperator()) { + default: + break; + case OO_New: + return OO_Delete; + case OO_Delete: + return OO_New; + case OO_Array_New: + return OO_Array_Delete; + case OO_Array_Delete: + return OO_Array_New; + } + llvm_unreachable("Not an overloaded allocation operator"); +} + +const char *getOperatorName(OverloadedOperatorKind K) { + switch (K) { + default: + break; + case OO_New: + return "operator new"; + case OO_Delete: + return "operator delete"; + case OO_Array_New: + return "operator new[]"; + case OO_Array_Delete: + return "operator delete[]"; + } + llvm_unreachable("Not an overloaded allocation operator"); +} + +bool areCorrespondingOverloads(const FunctionDecl *LHS, + const FunctionDecl *RHS) { + return RHS->getOverloadedOperator() == getCorrespondingOverload(LHS); +} + +bool hasCorrespondingOverloadInBaseClass(const CXXMethodDecl *MD, + const CXXRecordDecl *RD = nullptr) { + if (RD) { + // Check the methods in the given class and accessible to derived classes. + for (const auto *BMD : RD->methods()) + if (BMD->isOverloadedOperator() && BMD->getAccess() != AS_private && + areCorrespondingOverloads(MD, BMD)) + return true; + } else { + // Get the parent class of the method; we do not need to care about checking + // the methods in this class as the caller has already done that by looking + // at the declaration contexts. + RD = MD->getParent(); + } + + for (const auto &BS : RD->bases()) { + // We can't say much about a dependent base class, but to avoid false + // positives assume it can have a corresponding overload. + if (BS.getType()->isDependentType()) + return true; + if (const auto *BaseRD = BS.getType()->getAsCXXRecordDecl()) + if (hasCorrespondingOverloadInBaseClass(MD, BaseRD)) + return true; + } + + return false; +} + +} // anonymous namespace + +void NewDeleteOverloadsCheck::registerMatchers(MatchFinder *Finder) { + // Match all operator new and operator delete overloads (including the array + // forms). Do not match implicit operators, placement operators, or + // deleted/private operators. + // + // Technically, trivially-defined operator delete seems like a reasonable + // thing to also skip. e.g., void operator delete(void *) {} + // However, I think it's more reasonable to warn in this case as the user + // should really be writing that as a deleted function. + Finder->addMatcher( + functionDecl(unless(anyOf(isImplicit(), isPlacementOverload(), + isDeleted(), cxxMethodDecl(isPrivate()))), + anyOf(hasOverloadedOperatorName("new"), + hasOverloadedOperatorName("new[]"), + hasOverloadedOperatorName("delete"), + hasOverloadedOperatorName("delete[]"))) + .bind("func"), + this); +} + +void NewDeleteOverloadsCheck::check(const MatchFinder::MatchResult &Result) { + // Add any matches we locate to the list of things to be checked at the + // end of the translation unit. + const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func"); + const CXXRecordDecl *RD = nullptr; + if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) + RD = MD->getParent(); + Overloads[RD].push_back(FD); +} + +void NewDeleteOverloadsCheck::onEndOfTranslationUnit() { + // Walk over the list of declarations we've found to see if there is a + // corresponding overload at the same declaration context or within a base + // class. If there is not, add the element to the list of declarations to + // diagnose. + SmallVector<const FunctionDecl *, 4> Diagnose; + for (const auto &RP : Overloads) { + // We don't care about the CXXRecordDecl key in the map; we use it as a way + // to shard the overloads by declaration context to reduce the algorithmic + // complexity when searching for corresponding free store functions. + for (const auto *Overload : RP.second) { + const auto *Match = + std::find_if(RP.second.begin(), RP.second.end(), + [&Overload](const FunctionDecl *FD) { + if (FD == Overload) + return false; + // If the declaration contexts don't match, we don't + // need to check any further. + if (FD->getDeclContext() != Overload->getDeclContext()) + return false; + + // Since the declaration contexts match, see whether + // the current element is the corresponding operator. + if (!areCorrespondingOverloads(Overload, FD)) + return false; + + return true; + }); + + if (Match == RP.second.end()) { + // Check to see if there is a corresponding overload in a base class + // context. If there isn't, or if the overload is not a class member + // function, then we should diagnose. + const auto *MD = dyn_cast<CXXMethodDecl>(Overload); + if (!MD || !hasCorrespondingOverloadInBaseClass(MD)) + Diagnose.push_back(Overload); + } + } + } + + for (const auto *FD : Diagnose) + diag(FD->getLocation(), "declaration of %0 has no matching declaration " + "of '%1' at the same scope") + << FD << getOperatorName(getCorrespondingOverload(FD)); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.h new file mode 100644 index 0000000000..b11a57aebb --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NewDeleteOverloadsCheck.h @@ -0,0 +1,36 @@ +//===--- NewDeleteOverloadsCheck.h - clang-tidy----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NEWDELETEOVERLOADS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NEWDELETEOVERLOADS_H + +#include "../ClangTidyCheck.h" +#include "llvm/ADT/SmallVector.h" +#include <map> + +namespace clang::tidy::misc { + +class NewDeleteOverloadsCheck : public ClangTidyCheck { + std::map<const clang::CXXRecordDecl *, + llvm::SmallVector<const clang::FunctionDecl *, 4>> + Overloads; + +public: + NewDeleteOverloadsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NEWDELETEOVERLOADS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.cpp new file mode 100644 index 0000000000..2250bba4db --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.cpp @@ -0,0 +1,271 @@ +//===--- NoRecursionCheck.cpp - clang-tidy --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NoRecursionCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Analysis/CallGraph.h" +#include "llvm/ADT/DenseMapInfo.h" +#include "llvm/ADT/SCCIterator.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +/// Much like SmallSet, with two differences: +/// 1. It can *only* be constructed from an ArrayRef<>. If the element count +/// is small, there is no copy and said storage *must* outlive us. +/// 2. it is immutable, the way it was constructed it will stay. +template <typename T, unsigned SmallSize> class ImmutableSmallSet { + ArrayRef<T> Vector; + llvm::DenseSet<T> Set; + + static_assert(SmallSize <= 32, "N should be small"); + + bool isSmall() const { return Set.empty(); } + +public: + using size_type = size_t; + + ImmutableSmallSet() = delete; + ImmutableSmallSet(const ImmutableSmallSet &) = delete; + ImmutableSmallSet(ImmutableSmallSet &&) = delete; + T &operator=(const ImmutableSmallSet &) = delete; + T &operator=(ImmutableSmallSet &&) = delete; + + // WARNING: Storage *must* outlive us if we decide that the size is small. + ImmutableSmallSet(ArrayRef<T> Storage) { + // Is size small-enough to just keep using the existing storage? + if (Storage.size() <= SmallSize) { + Vector = Storage; + return; + } + + // We've decided that it isn't performant to keep using vector. + // Let's migrate the data into Set. + Set.reserve(Storage.size()); + Set.insert(Storage.begin(), Storage.end()); + } + + /// count - Return 1 if the element is in the set, 0 otherwise. + size_type count(const T &V) const { + if (isSmall()) { + // Since the collection is small, just do a linear search. + return llvm::is_contained(Vector, V) ? 1 : 0; + } + + return Set.count(V); + } +}; + +/// Much like SmallSetVector, but with one difference: +/// when the size is \p SmallSize or less, when checking whether an element is +/// already in the set or not, we perform linear search over the vector, +/// but if the size is larger than \p SmallSize, we look in set. +/// FIXME: upstream this into SetVector/SmallSetVector itself. +template <typename T, unsigned SmallSize> class SmartSmallSetVector { +public: + using size_type = size_t; + +private: + SmallVector<T, SmallSize> Vector; + llvm::DenseSet<T> Set; + + static_assert(SmallSize <= 32, "N should be small"); + + // Are we still using Vector for uniqness tracking? + bool isSmall() const { return Set.empty(); } + + // Will one more entry cause Vector to switch away from small-size storage? + bool entiretyOfVectorSmallSizeIsOccupied() const { + assert(isSmall() && Vector.size() <= SmallSize && + "Shouldn't ask if we have already [should have] migrated into Set."); + return Vector.size() == SmallSize; + } + + void populateSet() { + assert(Set.empty() && "Should not have already utilized the Set."); + // Magical growth factor prediction - to how many elements do we expect to + // sanely grow after switching away from small-size storage? + const size_t NewMaxElts = 4 * Vector.size(); + Vector.reserve(NewMaxElts); + Set.reserve(NewMaxElts); + Set.insert(Vector.begin(), Vector.end()); + } + + /// count - Return 1 if the element is in the set, 0 otherwise. + size_type count(const T &V) const { + if (isSmall()) { + // Since the collection is small, just do a linear search. + return llvm::is_contained(Vector, V) ? 1 : 0; + } + // Look-up in the Set. + return Set.count(V); + } + + bool setInsert(const T &V) { + if (count(V) != 0) + return false; // Already exists. + // Does not exist, Can/need to record it. + if (isSmall()) { // Are we still using Vector for uniqness tracking? + // Will one more entry fit within small-sized Vector? + if (!entiretyOfVectorSmallSizeIsOccupied()) + return true; // We'll insert into vector right afterwards anyway. + // Time to switch to Set. + populateSet(); + } + // Set time! + // Note that this must be after `populateSet()` might have been called. + bool SetInsertionSucceeded = Set.insert(V).second; + (void)SetInsertionSucceeded; + assert(SetInsertionSucceeded && "We did check that no such value existed"); + return true; + } + +public: + /// Insert a new element into the SmartSmallSetVector. + /// \returns true if the element was inserted into the SmartSmallSetVector. + bool insert(const T &X) { + bool Result = setInsert(X); + if (Result) + Vector.push_back(X); + return Result; + } + + /// Clear the SmartSmallSetVector and return the underlying vector. + decltype(Vector) takeVector() { + Set.clear(); + return std::move(Vector); + } +}; + +constexpr unsigned SmallCallStackSize = 16; +constexpr unsigned SmallSCCSize = 32; + +using CallStackTy = + llvm::SmallVector<CallGraphNode::CallRecord, SmallCallStackSize>; + +// In given SCC, find *some* call stack that will be cyclic. +// This will only find *one* such stack, it might not be the smallest one, +// and there may be other loops. +CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) { + // We'll need to be able to performantly look up whether some CallGraphNode + // is in SCC or not, so cache all the SCC elements in a set. + const ImmutableSmallSet<CallGraphNode *, SmallSCCSize> SCCElts(SCC); + + // Is node N part if the current SCC? + auto NodeIsPartOfSCC = [&SCCElts](CallGraphNode *N) { + return SCCElts.count(N) != 0; + }; + + // Track the call stack that will cause a cycle. + SmartSmallSetVector<CallGraphNode::CallRecord, SmallCallStackSize> + CallStackSet; + + // Arbitrarily take the first element of SCC as entry point. + CallGraphNode::CallRecord EntryNode(SCC.front(), /*CallExpr=*/nullptr); + // Continue recursing into subsequent callees that are part of this SCC, + // and are thus known to be part of the call graph loop, until loop forms. + CallGraphNode::CallRecord *Node = &EntryNode; + while (true) { + // Did we see this node before? + if (!CallStackSet.insert(*Node)) + break; // Cycle completed! Note that didn't insert the node into stack! + // Else, perform depth-first traversal: out of all callees, pick first one + // that is part of this SCC. This is not guaranteed to yield shortest cycle. + Node = llvm::find_if(Node->Callee->callees(), NodeIsPartOfSCC); + } + + // Note that we failed to insert the last node, that completes the cycle. + // But we really want to have it. So insert it manually into stack only. + CallStackTy CallStack = CallStackSet.takeVector(); + CallStack.emplace_back(*Node); + + return CallStack; +} + +} // namespace + +void NoRecursionCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(translationUnitDecl().bind("TUDecl"), this); +} + +void NoRecursionCheck::handleSCC(ArrayRef<CallGraphNode *> SCC) { + assert(!SCC.empty() && "Empty SCC does not make sense."); + + // First of all, call out every strongly connected function. + for (CallGraphNode *N : SCC) { + FunctionDecl *D = N->getDefinition(); + diag(D->getLocation(), "function %0 is within a recursive call chain") << D; + } + + // Now, SCC only tells us about strongly connected function declarations in + // the call graph. It doesn't *really* tell us about the cycles they form. + // And there may be more than one cycle in SCC. + // So let's form a call stack that eventually exposes *some* cycle. + const CallStackTy EventuallyCyclicCallStack = pathfindSomeCycle(SCC); + assert(!EventuallyCyclicCallStack.empty() && "We should've found the cycle"); + + // While last node of the call stack does cause a loop, due to the way we + // pathfind the cycle, the loop does not necessarily begin at the first node + // of the call stack, so drop front nodes of the call stack until it does. + const auto CyclicCallStack = + ArrayRef<CallGraphNode::CallRecord>(EventuallyCyclicCallStack) + .drop_until([LastNode = EventuallyCyclicCallStack.back()]( + CallGraphNode::CallRecord FrontNode) { + return FrontNode == LastNode; + }); + assert(CyclicCallStack.size() >= 2 && "Cycle requires at least 2 frames"); + + // Which function we decided to be the entry point that lead to the recursion? + FunctionDecl *CycleEntryFn = CyclicCallStack.front().Callee->getDefinition(); + // And now, for ease of understanding, let's print the call sequence that + // forms the cycle in question. + diag(CycleEntryFn->getLocation(), + "example recursive call chain, starting from function %0", + DiagnosticIDs::Note) + << CycleEntryFn; + for (int CurFrame = 1, NumFrames = CyclicCallStack.size(); + CurFrame != NumFrames; ++CurFrame) { + CallGraphNode::CallRecord PrevNode = CyclicCallStack[CurFrame - 1]; + CallGraphNode::CallRecord CurrNode = CyclicCallStack[CurFrame]; + + Decl *PrevDecl = PrevNode.Callee->getDecl(); + Decl *CurrDecl = CurrNode.Callee->getDecl(); + + diag(CurrNode.CallExpr->getBeginLoc(), + "Frame #%0: function %1 calls function %2 here:", DiagnosticIDs::Note) + << CurFrame << cast<NamedDecl>(PrevDecl) << cast<NamedDecl>(CurrDecl); + } + + diag(CyclicCallStack.back().CallExpr->getBeginLoc(), + "... which was the starting point of the recursive call chain; there " + "may be other cycles", + DiagnosticIDs::Note); +} + +void NoRecursionCheck::check(const MatchFinder::MatchResult &Result) { + // Build call graph for the entire translation unit. + const auto *TU = Result.Nodes.getNodeAs<TranslationUnitDecl>("TUDecl"); + CallGraph CG; + CG.addToCallGraph(const_cast<TranslationUnitDecl *>(TU)); + + // Look for cycles in call graph, + // by looking for Strongly Connected Components (SCC's) + for (llvm::scc_iterator<CallGraph *> SCCI = llvm::scc_begin(&CG), + SCCE = llvm::scc_end(&CG); + SCCI != SCCE; ++SCCI) { + if (!SCCI.hasCycle()) // We only care about cycles, not standalone nodes. + continue; + handleSCC(*SCCI); + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.h new file mode 100644 index 0000000000..fd82ffc6e5 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NoRecursionCheck.h @@ -0,0 +1,40 @@ +//===--- NoRecursionCheck.h - clang-tidy ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NORECURSIONCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NORECURSIONCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { + +class CallGraphNode; + +namespace tidy::misc { + +/// Finds strongly connected functions (by analyzing call graph for SCC's +/// that are loops), diagnoses each function in the cycle, +/// and displays one example of possible call graph loop (recursion). +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/no-recursion.html +class NoRecursionCheck : public ClangTidyCheck { +public: + NoRecursionCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + void handleSCC(ArrayRef<CallGraphNode *> SCC); +}; + +} // namespace tidy::misc +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NORECURSIONCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp new file mode 100644 index 0000000000..4c415a7fd2 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.cpp @@ -0,0 +1,68 @@ +//===--- NonCopyableObjects.cpp - clang-tidy-------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NonCopyableObjects.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include <algorithm> + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void NonCopyableObjectsCheck::registerMatchers(MatchFinder *Finder) { + // There are two ways to get into trouble with objects like FILE *: + // dereferencing the pointer type to be a non-pointer type, and declaring + // the type as a non-pointer type in the first place. While the declaration + // itself could technically be well-formed in the case where the type is not + // an opaque type, it's highly suspicious behavior. + // + // POSIX types are a bit different in that it's reasonable to declare a + // non-pointer variable or data member of the type, but it is not reasonable + // to dereference a pointer to the type, or declare a parameter of non-pointer + // type. + // FIXME: it would be good to make a list that is also user-configurable so + // that users can add their own elements to the list. However, it may require + // some extra thought since POSIX types and FILE types are usable in different + // ways. + + auto BadFILEType = hasType( + namedDecl(hasAnyName("::FILE", "FILE", "std::FILE")).bind("type_decl")); + auto BadPOSIXType = + hasType(namedDecl(hasAnyName("::pthread_cond_t", "::pthread_mutex_t", + "pthread_cond_t", "pthread_mutex_t")) + .bind("type_decl")); + auto BadEitherType = anyOf(BadFILEType, BadPOSIXType); + + Finder->addMatcher( + namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType))) + .bind("decl"), + this); + Finder->addMatcher(parmVarDecl(BadPOSIXType).bind("decl"), this); + Finder->addMatcher( + expr(unaryOperator(hasOperatorName("*"), BadEitherType)).bind("expr"), + this); +} + +void NonCopyableObjectsCheck::check(const MatchFinder::MatchResult &Result) { + const auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl"); + const auto *BD = Result.Nodes.getNodeAs<NamedDecl>("type_decl"); + const auto *E = Result.Nodes.getNodeAs<Expr>("expr"); + + if (D && BD) + diag(D->getLocation(), "%0 declared as type '%1', which is unsafe to copy" + "; did you mean '%1 *'?") + << D << BD->getName(); + else if (E) + diag(E->getExprLoc(), + "expression has opaque data structure type %0; type should only be " + "used as a pointer and not dereferenced") + << BD; +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.h new file mode 100644 index 0000000000..b886ea9484 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonCopyableObjects.h @@ -0,0 +1,28 @@ +//===--- NonCopyableObjects.h - clang-tidy-----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONCOPYABLEOBJECTS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONCOPYABLEOBJECTS_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// The check flags dereferences and non-pointer declarations of objects that +/// are not meant to be passed by value, such as C FILE objects. +class NonCopyableObjectsCheck : public ClangTidyCheck { +public: + NonCopyableObjectsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONCOPYABLEOBJECTS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp new file mode 100644 index 0000000000..9d7d9d1f86 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp @@ -0,0 +1,93 @@ +//===--- NonPrivateMemberVariablesInClassesCheck.cpp - clang-tidy ---------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "NonPrivateMemberVariablesInClassesCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +AST_MATCHER(CXXRecordDecl, hasMethods) { + return std::distance(Node.method_begin(), Node.method_end()) != 0; +} + +AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) { + return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit()))) + .matches(Node, Finder, Builder); +} + +AST_MATCHER(CXXRecordDecl, hasNonPublicMemberVariable) { + return cxxRecordDecl(has(fieldDecl(unless(isPublic())))) + .matches(Node, Finder, Builder); +} + +AST_POLYMORPHIC_MATCHER_P(boolean, AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl), + bool, Boolean) { + return Boolean; +} + +} // namespace + +NonPrivateMemberVariablesInClassesCheck:: + NonPrivateMemberVariablesInClassesCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + IgnoreClassesWithAllMemberVariablesBeingPublic( + Options.get("IgnoreClassesWithAllMemberVariablesBeingPublic", false)), + IgnorePublicMemberVariables( + Options.get("IgnorePublicMemberVariables", false)) {} + +void NonPrivateMemberVariablesInClassesCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoreClassesWithAllMemberVariablesBeingPublic", + IgnoreClassesWithAllMemberVariablesBeingPublic); + Options.store(Opts, "IgnorePublicMemberVariables", + IgnorePublicMemberVariables); +} + +void NonPrivateMemberVariablesInClassesCheck::registerMatchers( + MatchFinder *Finder) { + // We can ignore structs/classes with all member variables being public. + auto ShouldIgnoreRecord = + allOf(boolean(IgnoreClassesWithAllMemberVariablesBeingPublic), + unless(hasNonPublicMemberVariable())); + + // There are three visibility types: public, protected, private. + // If we are ok with public fields, then we only want to complain about + // protected fields, else we want to complain about all non-private fields. + // We can ignore public member variables in structs/classes, in unions. + auto InterestingField = IgnorePublicMemberVariables + ? fieldDecl(isProtected()) + : fieldDecl(unless(isPrivate())); + + // We only want the records that not only contain the mutable data (non-static + // member variables), but also have some logic (non-static, non-implicit + // member functions). We may optionally ignore records where all the member + // variables are public. + Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(), + hasNonStaticNonImplicitMethod(), + unless(ShouldIgnoreRecord), + forEach(InterestingField.bind("field"))) + .bind("record"), + this); +} + +void NonPrivateMemberVariablesInClassesCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *Field = Result.Nodes.getNodeAs<FieldDecl>("field"); + assert(Field && "We should have the field we are going to complain about"); + + diag(Field->getLocation(), "member variable %0 has %1 visibility") + << Field << Field->getAccess(); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.h new file mode 100644 index 0000000000..5f0687abdd --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.h @@ -0,0 +1,45 @@ +//===--- NonPrivateMemberVariablesInClassesCheck.h - clang-tidy -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONPRIVATEMEMBERVARIABLESINCLASSESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONPRIVATEMEMBERVARIABLESINCLASSESCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// This checker finds classes that not only contain the data +/// (non-static member variables), but also have logic (non-static member +/// functions), and diagnoses all member variables that have any other scope +/// other than `private`. They should be made `private`, and manipulated +/// exclusively via the member functions. +/// +/// Optionally, classes with all member variables being `public` could be +/// ignored and optionally all `public` member variables could be ignored. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/non-private-member-variables-in-classes.html +class NonPrivateMemberVariablesInClassesCheck : public ClangTidyCheck { +public: + NonPrivateMemberVariablesInClassesCheck(StringRef Name, + ClangTidyContext *Context); + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool IgnoreClassesWithAllMemberVariablesBeingPublic; + const bool IgnorePublicMemberVariables; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_NONPRIVATEMEMBERVARIABLESINCLASSESCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.cpp new file mode 100644 index 0000000000..b5028074f0 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.cpp @@ -0,0 +1,1358 @@ +//===--- RedundantExpressionCheck.cpp - clang-tidy-------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "RedundantExpressionCheck.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/ExprConcepts.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/LLVM.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/APSInt.h" +#include "llvm/ADT/FoldingSet.h" +#include "llvm/ADT/SmallBitVector.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/FormatVariadic.h" +#include <algorithm> +#include <cassert> +#include <cstdint> +#include <optional> +#include <string> +#include <vector> + +using namespace clang::ast_matchers; +using namespace clang::tidy::matchers; + +namespace clang::tidy::misc { +namespace { +using llvm::APSInt; + +static constexpr llvm::StringLiteral KnownBannedMacroNames[] = { + "EAGAIN", + "EWOULDBLOCK", + "SIGCLD", + "SIGCHLD", +}; + +static bool incrementWithoutOverflow(const APSInt &Value, APSInt &Result) { + Result = Value; + ++Result; + return Value < Result; +} + +static bool areEquivalentNameSpecifier(const NestedNameSpecifier *Left, + const NestedNameSpecifier *Right) { + llvm::FoldingSetNodeID LeftID, RightID; + Left->Profile(LeftID); + Right->Profile(RightID); + return LeftID == RightID; +} + +static bool areEquivalentExpr(const Expr *Left, const Expr *Right) { + if (!Left || !Right) + return !Left && !Right; + + Left = Left->IgnoreParens(); + Right = Right->IgnoreParens(); + + // Compare classes. + if (Left->getStmtClass() != Right->getStmtClass()) + return false; + + // Compare children. + Expr::const_child_iterator LeftIter = Left->child_begin(); + Expr::const_child_iterator RightIter = Right->child_begin(); + while (LeftIter != Left->child_end() && RightIter != Right->child_end()) { + if (!areEquivalentExpr(dyn_cast_or_null<Expr>(*LeftIter), + dyn_cast_or_null<Expr>(*RightIter))) + return false; + ++LeftIter; + ++RightIter; + } + if (LeftIter != Left->child_end() || RightIter != Right->child_end()) + return false; + + // Perform extra checks. + switch (Left->getStmtClass()) { + default: + return false; + + case Stmt::CharacterLiteralClass: + return cast<CharacterLiteral>(Left)->getValue() == + cast<CharacterLiteral>(Right)->getValue(); + case Stmt::IntegerLiteralClass: { + llvm::APInt LeftLit = cast<IntegerLiteral>(Left)->getValue(); + llvm::APInt RightLit = cast<IntegerLiteral>(Right)->getValue(); + return LeftLit.getBitWidth() == RightLit.getBitWidth() && + LeftLit == RightLit; + } + case Stmt::FloatingLiteralClass: + return cast<FloatingLiteral>(Left)->getValue().bitwiseIsEqual( + cast<FloatingLiteral>(Right)->getValue()); + case Stmt::StringLiteralClass: + return cast<StringLiteral>(Left)->getBytes() == + cast<StringLiteral>(Right)->getBytes(); + case Stmt::CXXOperatorCallExprClass: + return cast<CXXOperatorCallExpr>(Left)->getOperator() == + cast<CXXOperatorCallExpr>(Right)->getOperator(); + case Stmt::DependentScopeDeclRefExprClass: + if (cast<DependentScopeDeclRefExpr>(Left)->getDeclName() != + cast<DependentScopeDeclRefExpr>(Right)->getDeclName()) + return false; + return areEquivalentNameSpecifier( + cast<DependentScopeDeclRefExpr>(Left)->getQualifier(), + cast<DependentScopeDeclRefExpr>(Right)->getQualifier()); + case Stmt::DeclRefExprClass: + return cast<DeclRefExpr>(Left)->getDecl() == + cast<DeclRefExpr>(Right)->getDecl(); + case Stmt::MemberExprClass: + return cast<MemberExpr>(Left)->getMemberDecl() == + cast<MemberExpr>(Right)->getMemberDecl(); + case Stmt::CXXFoldExprClass: + return cast<CXXFoldExpr>(Left)->getOperator() == + cast<CXXFoldExpr>(Right)->getOperator(); + case Stmt::CXXFunctionalCastExprClass: + case Stmt::CStyleCastExprClass: + return cast<ExplicitCastExpr>(Left)->getTypeAsWritten() == + cast<ExplicitCastExpr>(Right)->getTypeAsWritten(); + case Stmt::CallExprClass: + case Stmt::ImplicitCastExprClass: + case Stmt::ArraySubscriptExprClass: + return true; + case Stmt::UnaryOperatorClass: + if (cast<UnaryOperator>(Left)->isIncrementDecrementOp()) + return false; + return cast<UnaryOperator>(Left)->getOpcode() == + cast<UnaryOperator>(Right)->getOpcode(); + case Stmt::BinaryOperatorClass: + if (cast<BinaryOperator>(Left)->isAssignmentOp()) + return false; + return cast<BinaryOperator>(Left)->getOpcode() == + cast<BinaryOperator>(Right)->getOpcode(); + case Stmt::UnaryExprOrTypeTraitExprClass: + const auto *LeftUnaryExpr = + cast<UnaryExprOrTypeTraitExpr>(Left); + const auto *RightUnaryExpr = + cast<UnaryExprOrTypeTraitExpr>(Right); + if (LeftUnaryExpr->isArgumentType() && RightUnaryExpr->isArgumentType()) + return LeftUnaryExpr->getArgumentType() == + RightUnaryExpr->getArgumentType(); + if (!LeftUnaryExpr->isArgumentType() && !RightUnaryExpr->isArgumentType()) + return areEquivalentExpr(LeftUnaryExpr->getArgumentExpr(), + RightUnaryExpr->getArgumentExpr()); + + return false; + } +} + +// For a given expression 'x', returns whether the ranges covered by the +// relational operators are equivalent (i.e. x <= 4 is equivalent to x < 5). +static bool areEquivalentRanges(BinaryOperatorKind OpcodeLHS, + const APSInt &ValueLHS, + BinaryOperatorKind OpcodeRHS, + const APSInt &ValueRHS) { + assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 && + "Values must be ordered"); + // Handle the case where constants are the same: x <= 4 <==> x <= 4. + if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) + return OpcodeLHS == OpcodeRHS; + + // Handle the case where constants are off by one: x <= 4 <==> x < 5. + APSInt ValueLhsPlus1; + return ((OpcodeLHS == BO_LE && OpcodeRHS == BO_LT) || + (OpcodeLHS == BO_GT && OpcodeRHS == BO_GE)) && + incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) && + APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0; +} + +// For a given expression 'x', returns whether the ranges covered by the +// relational operators are fully disjoint (i.e. x < 4 and x > 7). +static bool areExclusiveRanges(BinaryOperatorKind OpcodeLHS, + const APSInt &ValueLHS, + BinaryOperatorKind OpcodeRHS, + const APSInt &ValueRHS) { + assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 && + "Values must be ordered"); + + // Handle cases where the constants are the same. + if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) { + switch (OpcodeLHS) { + case BO_EQ: + return OpcodeRHS == BO_NE || OpcodeRHS == BO_GT || OpcodeRHS == BO_LT; + case BO_NE: + return OpcodeRHS == BO_EQ; + case BO_LE: + return OpcodeRHS == BO_GT; + case BO_GE: + return OpcodeRHS == BO_LT; + case BO_LT: + return OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE; + case BO_GT: + return OpcodeRHS == BO_EQ || OpcodeRHS == BO_LT || OpcodeRHS == BO_LE; + default: + return false; + } + } + + // Handle cases where the constants are different. + if ((OpcodeLHS == BO_EQ || OpcodeLHS == BO_LT || OpcodeLHS == BO_LE) && + (OpcodeRHS == BO_EQ || OpcodeRHS == BO_GT || OpcodeRHS == BO_GE)) + return true; + + // Handle the case where constants are off by one: x > 5 && x < 6. + APSInt ValueLhsPlus1; + if (OpcodeLHS == BO_GT && OpcodeRHS == BO_LT && + incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) && + APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0) + return true; + + return false; +} + +// Returns whether the ranges covered by the union of both relational +// expressions cover the whole domain (i.e. x < 10 and x > 0). +static bool rangesFullyCoverDomain(BinaryOperatorKind OpcodeLHS, + const APSInt &ValueLHS, + BinaryOperatorKind OpcodeRHS, + const APSInt &ValueRHS) { + assert(APSInt::compareValues(ValueLHS, ValueRHS) <= 0 && + "Values must be ordered"); + + // Handle cases where the constants are the same: x < 5 || x >= 5. + if (APSInt::compareValues(ValueLHS, ValueRHS) == 0) { + switch (OpcodeLHS) { + case BO_EQ: + return OpcodeRHS == BO_NE; + case BO_NE: + return OpcodeRHS == BO_EQ; + case BO_LE: + return OpcodeRHS == BO_GT || OpcodeRHS == BO_GE; + case BO_LT: + return OpcodeRHS == BO_GE; + case BO_GE: + return OpcodeRHS == BO_LT || OpcodeRHS == BO_LE; + case BO_GT: + return OpcodeRHS == BO_LE; + default: + return false; + } + } + + // Handle the case where constants are off by one: x <= 4 || x >= 5. + APSInt ValueLhsPlus1; + if (OpcodeLHS == BO_LE && OpcodeRHS == BO_GE && + incrementWithoutOverflow(ValueLHS, ValueLhsPlus1) && + APSInt::compareValues(ValueLhsPlus1, ValueRHS) == 0) + return true; + + // Handle cases where the constants are different: x > 4 || x <= 7. + if ((OpcodeLHS == BO_GT || OpcodeLHS == BO_GE) && + (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE)) + return true; + + // Handle cases where constants are different but both ops are !=, like: + // x != 5 || x != 10 + if (OpcodeLHS == BO_NE && OpcodeRHS == BO_NE) + return true; + + return false; +} + +static bool rangeSubsumesRange(BinaryOperatorKind OpcodeLHS, + const APSInt &ValueLHS, + BinaryOperatorKind OpcodeRHS, + const APSInt &ValueRHS) { + int Comparison = APSInt::compareValues(ValueLHS, ValueRHS); + switch (OpcodeLHS) { + case BO_EQ: + return OpcodeRHS == BO_EQ && Comparison == 0; + case BO_NE: + return (OpcodeRHS == BO_NE && Comparison == 0) || + (OpcodeRHS == BO_EQ && Comparison != 0) || + (OpcodeRHS == BO_LT && Comparison >= 0) || + (OpcodeRHS == BO_LE && Comparison > 0) || + (OpcodeRHS == BO_GT && Comparison <= 0) || + (OpcodeRHS == BO_GE && Comparison < 0); + + case BO_LT: + return ((OpcodeRHS == BO_LT && Comparison >= 0) || + (OpcodeRHS == BO_LE && Comparison > 0) || + (OpcodeRHS == BO_EQ && Comparison > 0)); + case BO_GT: + return ((OpcodeRHS == BO_GT && Comparison <= 0) || + (OpcodeRHS == BO_GE && Comparison < 0) || + (OpcodeRHS == BO_EQ && Comparison < 0)); + case BO_LE: + return (OpcodeRHS == BO_LT || OpcodeRHS == BO_LE || OpcodeRHS == BO_EQ) && + Comparison >= 0; + case BO_GE: + return (OpcodeRHS == BO_GT || OpcodeRHS == BO_GE || OpcodeRHS == BO_EQ) && + Comparison <= 0; + default: + return false; + } +} + +static void transformSubToCanonicalAddExpr(BinaryOperatorKind &Opcode, + APSInt &Value) { + if (Opcode == BO_Sub) { + Opcode = BO_Add; + Value = -Value; + } +} + +// to use in the template below +static OverloadedOperatorKind getOp(const BinaryOperator *Op) { + return BinaryOperator::getOverloadedOperator(Op->getOpcode()); +} + +static OverloadedOperatorKind getOp(const CXXOperatorCallExpr *Op) { + if (Op->getNumArgs() != 2) + return OO_None; + return Op->getOperator(); +} + +static std::pair<const Expr *, const Expr *> +getOperands(const BinaryOperator *Op) { + return {Op->getLHS()->IgnoreParenImpCasts(), + Op->getRHS()->IgnoreParenImpCasts()}; +} + +static std::pair<const Expr *, const Expr *> +getOperands(const CXXOperatorCallExpr *Op) { + return {Op->getArg(0)->IgnoreParenImpCasts(), + Op->getArg(1)->IgnoreParenImpCasts()}; +} + +template <typename TExpr> +static const TExpr *checkOpKind(const Expr *TheExpr, + OverloadedOperatorKind OpKind) { + const auto *AsTExpr = dyn_cast_or_null<TExpr>(TheExpr); + if (AsTExpr && getOp(AsTExpr) == OpKind) + return AsTExpr; + + return nullptr; +} + +// returns true if a subexpression has two directly equivalent operands and +// is already handled by operands/parametersAreEquivalent +template <typename TExpr, unsigned N> +static bool collectOperands(const Expr *Part, + SmallVector<const Expr *, N> &AllOperands, + OverloadedOperatorKind OpKind) { + if (const auto *BinOp = checkOpKind<TExpr>(Part, OpKind)) { + const std::pair<const Expr *, const Expr *> Operands = getOperands(BinOp); + if (areEquivalentExpr(Operands.first, Operands.second)) + return true; + return collectOperands<TExpr>(Operands.first, AllOperands, OpKind) || + collectOperands<TExpr>(Operands.second, AllOperands, OpKind); + } + + AllOperands.push_back(Part); + return false; +} + +template <typename TExpr> +static bool hasSameOperatorParent(const Expr *TheExpr, + OverloadedOperatorKind OpKind, + ASTContext &Context) { + // IgnoreParenImpCasts logic in reverse: skip surrounding uninteresting nodes + const DynTypedNodeList Parents = Context.getParents(*TheExpr); + for (DynTypedNode DynParent : Parents) { + if (const auto *Parent = DynParent.get<Expr>()) { + bool Skip = isa<ParenExpr>(Parent) || isa<ImplicitCastExpr>(Parent) || + isa<FullExpr>(Parent) || + isa<MaterializeTemporaryExpr>(Parent); + if (Skip && hasSameOperatorParent<TExpr>(Parent, OpKind, Context)) + return true; + if (checkOpKind<TExpr>(Parent, OpKind)) + return true; + } + } + + return false; +} + +template <typename TExpr> +static bool +markDuplicateOperands(const TExpr *TheExpr, + ast_matchers::internal::BoundNodesTreeBuilder *Builder, + ASTContext &Context) { + const OverloadedOperatorKind OpKind = getOp(TheExpr); + if (OpKind == OO_None) + return false; + // if there are no nested operators of the same kind, it's handled by + // operands/parametersAreEquivalent + const std::pair<const Expr *, const Expr *> Operands = getOperands(TheExpr); + if (!(checkOpKind<TExpr>(Operands.first, OpKind) || + checkOpKind<TExpr>(Operands.second, OpKind))) + return false; + + // if parent is the same kind of operator, it's handled by a previous call to + // markDuplicateOperands + if (hasSameOperatorParent<TExpr>(TheExpr, OpKind, Context)) + return false; + + SmallVector<const Expr *, 4> AllOperands; + if (collectOperands<TExpr>(Operands.first, AllOperands, OpKind)) + return false; + if (collectOperands<TExpr>(Operands.second, AllOperands, OpKind)) + return false; + size_t NumOperands = AllOperands.size(); + llvm::SmallBitVector Duplicates(NumOperands); + for (size_t I = 0; I < NumOperands; I++) { + if (Duplicates[I]) + continue; + bool FoundDuplicates = false; + + for (size_t J = I + 1; J < NumOperands; J++) { + if (AllOperands[J]->HasSideEffects(Context)) + break; + + if (areEquivalentExpr(AllOperands[I], AllOperands[J])) { + FoundDuplicates = true; + Duplicates.set(J); + Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", J)), + DynTypedNode::create(*AllOperands[J])); + } + } + + if (FoundDuplicates) + Builder->setBinding(SmallString<11>(llvm::formatv("duplicate{0}", I)), + DynTypedNode::create(*AllOperands[I])); + } + + return Duplicates.any(); +} + +AST_MATCHER(Expr, isIntegerConstantExpr) { + if (Node.isInstantiationDependent()) + return false; + return Node.isIntegerConstantExpr(Finder->getASTContext()); +} + +AST_MATCHER(Expr, isRequiresExpr) { return isa<RequiresExpr>(Node); } + +AST_MATCHER(BinaryOperator, operandsAreEquivalent) { + return areEquivalentExpr(Node.getLHS(), Node.getRHS()); +} + +AST_MATCHER(BinaryOperator, nestedOperandsAreEquivalent) { + return markDuplicateOperands(&Node, Builder, Finder->getASTContext()); +} + +AST_MATCHER(ConditionalOperator, expressionsAreEquivalent) { + return areEquivalentExpr(Node.getTrueExpr(), Node.getFalseExpr()); +} + +AST_MATCHER(CallExpr, parametersAreEquivalent) { + return Node.getNumArgs() == 2 && + areEquivalentExpr(Node.getArg(0), Node.getArg(1)); +} + +AST_MATCHER(CXXOperatorCallExpr, nestedParametersAreEquivalent) { + return markDuplicateOperands(&Node, Builder, Finder->getASTContext()); +} + +AST_MATCHER(BinaryOperator, binaryOperatorIsInMacro) { + return Node.getOperatorLoc().isMacroID(); +} + +AST_MATCHER(ConditionalOperator, conditionalOperatorIsInMacro) { + return Node.getQuestionLoc().isMacroID() || Node.getColonLoc().isMacroID(); +} + +AST_MATCHER(Expr, isMacro) { return Node.getExprLoc().isMacroID(); } + +AST_MATCHER_P(Expr, expandedByMacro, ArrayRef<llvm::StringLiteral>, Names) { + const SourceManager &SM = Finder->getASTContext().getSourceManager(); + const LangOptions &LO = Finder->getASTContext().getLangOpts(); + SourceLocation Loc = Node.getExprLoc(); + while (Loc.isMacroID()) { + StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LO); + if (llvm::is_contained(Names, MacroName)) + return true; + Loc = SM.getImmediateMacroCallerLoc(Loc); + } + return false; +} + +// Returns a matcher for integer constant expressions. +static ast_matchers::internal::Matcher<Expr> +matchIntegerConstantExpr(StringRef Id) { + std::string CstId = (Id + "-const").str(); + return expr(isIntegerConstantExpr()).bind(CstId); +} + +// Retrieves the integer expression matched by 'matchIntegerConstantExpr' with +// name 'Id' and stores it into 'ConstExpr', the value of the expression is +// stored into `Value`. +static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result, + StringRef Id, APSInt &Value, + const Expr *&ConstExpr) { + std::string CstId = (Id + "-const").str(); + ConstExpr = Result.Nodes.getNodeAs<Expr>(CstId); + if (!ConstExpr) + return false; + std::optional<llvm::APSInt> R = + ConstExpr->getIntegerConstantExpr(*Result.Context); + if (!R) + return false; + Value = *R; + return true; +} + +// Overloaded `retrieveIntegerConstantExpr` for compatibility. +static bool retrieveIntegerConstantExpr(const MatchFinder::MatchResult &Result, + StringRef Id, APSInt &Value) { + const Expr *ConstExpr = nullptr; + return retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr); +} + +// Returns a matcher for symbolic expressions (matches every expression except +// ingeter constant expressions). +static ast_matchers::internal::Matcher<Expr> matchSymbolicExpr(StringRef Id) { + std::string SymId = (Id + "-sym").str(); + return ignoringParenImpCasts( + expr(unless(isIntegerConstantExpr())).bind(SymId)); +} + +// Retrieves the expression matched by 'matchSymbolicExpr' with name 'Id' and +// stores it into 'SymExpr'. +static bool retrieveSymbolicExpr(const MatchFinder::MatchResult &Result, + StringRef Id, const Expr *&SymExpr) { + std::string SymId = (Id + "-sym").str(); + if (const auto *Node = Result.Nodes.getNodeAs<Expr>(SymId)) { + SymExpr = Node; + return true; + } + return false; +} + +// Match a binary operator between a symbolic expression and an integer constant +// expression. +static ast_matchers::internal::Matcher<Expr> +matchBinOpIntegerConstantExpr(StringRef Id) { + const auto BinOpCstExpr = + expr(anyOf(binaryOperator(hasAnyOperatorName("+", "|", "&"), + hasOperands(matchSymbolicExpr(Id), + matchIntegerConstantExpr(Id))), + binaryOperator(hasOperatorName("-"), + hasLHS(matchSymbolicExpr(Id)), + hasRHS(matchIntegerConstantExpr(Id))))) + .bind(Id); + return ignoringParenImpCasts(BinOpCstExpr); +} + +// Retrieves sub-expressions matched by 'matchBinOpIntegerConstantExpr' with +// name 'Id'. +static bool +retrieveBinOpIntegerConstantExpr(const MatchFinder::MatchResult &Result, + StringRef Id, BinaryOperatorKind &Opcode, + const Expr *&Symbol, APSInt &Value) { + if (const auto *BinExpr = Result.Nodes.getNodeAs<BinaryOperator>(Id)) { + Opcode = BinExpr->getOpcode(); + return retrieveSymbolicExpr(Result, Id, Symbol) && + retrieveIntegerConstantExpr(Result, Id, Value); + } + return false; +} + +// Matches relational expressions: 'Expr <op> k' (i.e. x < 2, x != 3, 12 <= x). +static ast_matchers::internal::Matcher<Expr> +matchRelationalIntegerConstantExpr(StringRef Id) { + std::string CastId = (Id + "-cast").str(); + std::string SwapId = (Id + "-swap").str(); + std::string NegateId = (Id + "-negate").str(); + std::string OverloadId = (Id + "-overload").str(); + std::string ConstId = (Id + "-const").str(); + + const auto RelationalExpr = ignoringParenImpCasts(binaryOperator( + isComparisonOperator(), expr().bind(Id), + anyOf(allOf(hasLHS(matchSymbolicExpr(Id)), + hasRHS(matchIntegerConstantExpr(Id))), + allOf(hasLHS(matchIntegerConstantExpr(Id)), + hasRHS(matchSymbolicExpr(Id)), expr().bind(SwapId))))); + + // A cast can be matched as a comparator to zero. (i.e. if (x) is equivalent + // to if (x != 0)). + const auto CastExpr = + implicitCastExpr(hasCastKind(CK_IntegralToBoolean), + hasSourceExpression(matchSymbolicExpr(Id))) + .bind(CastId); + + const auto NegateRelationalExpr = + unaryOperator(hasOperatorName("!"), + hasUnaryOperand(anyOf(CastExpr, RelationalExpr))) + .bind(NegateId); + + // Do not bind to double negation. + const auto NegateNegateRelationalExpr = + unaryOperator(hasOperatorName("!"), + hasUnaryOperand(unaryOperator( + hasOperatorName("!"), + hasUnaryOperand(anyOf(CastExpr, RelationalExpr))))); + + const auto OverloadedOperatorExpr = + cxxOperatorCallExpr( + hasAnyOverloadedOperatorName("==", "!=", "<", "<=", ">", ">="), + // Filter noisy false positives. + unless(isMacro()), unless(isInTemplateInstantiation()), + anyOf(hasLHS(ignoringParenImpCasts(integerLiteral().bind(ConstId))), + hasRHS(ignoringParenImpCasts(integerLiteral().bind(ConstId))))) + .bind(OverloadId); + + return anyOf(RelationalExpr, CastExpr, NegateRelationalExpr, + NegateNegateRelationalExpr, OverloadedOperatorExpr); +} + +// Checks whether a function param is non constant reference type, and may +// be modified in the function. +static bool isNonConstReferenceType(QualType ParamType) { + return ParamType->isReferenceType() && + !ParamType.getNonReferenceType().isConstQualified(); +} + +// Checks whether the arguments of an overloaded operator can be modified in the +// function. +// For operators that take an instance and a constant as arguments, only the +// first argument (the instance) needs to be checked, since the constant itself +// is a temporary expression. Whether the second parameter is checked is +// controlled by the parameter `ParamsToCheckCount`. +static bool +canOverloadedOperatorArgsBeModified(const CXXOperatorCallExpr *OperatorCall, + bool CheckSecondParam) { + const auto *OperatorDecl = + dyn_cast_or_null<FunctionDecl>(OperatorCall->getCalleeDecl()); + // if we can't find the declaration, conservatively assume it can modify + // arguments + if (!OperatorDecl) + return true; + + unsigned ParamCount = OperatorDecl->getNumParams(); + + // Overloaded operators declared inside a class have only one param. + // These functions must be declared const in order to not be able to modify + // the instance of the class they are called through. + if (ParamCount == 1 && + !OperatorDecl->getType()->castAs<FunctionType>()->isConst()) + return true; + + if (isNonConstReferenceType(OperatorDecl->getParamDecl(0)->getType())) + return true; + + return CheckSecondParam && ParamCount == 2 && + isNonConstReferenceType(OperatorDecl->getParamDecl(1)->getType()); +} + +// Retrieves sub-expressions matched by 'matchRelationalIntegerConstantExpr' +// with name 'Id'. +static bool retrieveRelationalIntegerConstantExpr( + const MatchFinder::MatchResult &Result, StringRef Id, + const Expr *&OperandExpr, BinaryOperatorKind &Opcode, const Expr *&Symbol, + APSInt &Value, const Expr *&ConstExpr) { + std::string CastId = (Id + "-cast").str(); + std::string SwapId = (Id + "-swap").str(); + std::string NegateId = (Id + "-negate").str(); + std::string OverloadId = (Id + "-overload").str(); + + if (const auto *Bin = Result.Nodes.getNodeAs<BinaryOperator>(Id)) { + // Operand received with explicit comparator. + Opcode = Bin->getOpcode(); + OperandExpr = Bin; + + if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr)) + return false; + } else if (const auto *Cast = Result.Nodes.getNodeAs<CastExpr>(CastId)) { + // Operand received with implicit comparator (cast). + Opcode = BO_NE; + OperandExpr = Cast; + Value = APSInt(32, false); + } else if (const auto *OverloadedOperatorExpr = + Result.Nodes.getNodeAs<CXXOperatorCallExpr>(OverloadId)) { + if (canOverloadedOperatorArgsBeModified(OverloadedOperatorExpr, false)) + return false; + + bool IntegerConstantIsFirstArg = false; + + if (const auto *Arg = OverloadedOperatorExpr->getArg(1)) { + if (!Arg->isValueDependent() && + !Arg->isIntegerConstantExpr(*Result.Context)) { + IntegerConstantIsFirstArg = true; + if (const auto *Arg = OverloadedOperatorExpr->getArg(0)) { + if (!Arg->isValueDependent() && + !Arg->isIntegerConstantExpr(*Result.Context)) + return false; + } else + return false; + } + } else + return false; + + Symbol = OverloadedOperatorExpr->getArg(IntegerConstantIsFirstArg ? 1 : 0); + OperandExpr = OverloadedOperatorExpr; + Opcode = BinaryOperator::getOverloadedOpcode(OverloadedOperatorExpr->getOperator()); + + if (!retrieveIntegerConstantExpr(Result, Id, Value, ConstExpr)) + return false; + + if (!BinaryOperator::isComparisonOp(Opcode)) + return false; + + // The call site of this function expects the constant on the RHS, + // so change the opcode accordingly. + if (IntegerConstantIsFirstArg) + Opcode = BinaryOperator::reverseComparisonOp(Opcode); + + return true; + } else { + return false; + } + + if (!retrieveSymbolicExpr(Result, Id, Symbol)) + return false; + + if (Result.Nodes.getNodeAs<Expr>(SwapId)) + Opcode = BinaryOperator::reverseComparisonOp(Opcode); + if (Result.Nodes.getNodeAs<Expr>(NegateId)) + Opcode = BinaryOperator::negateComparisonOp(Opcode); + return true; +} + +// Checks for expressions like (X == 4) && (Y != 9) +static bool areSidesBinaryConstExpressions(const BinaryOperator *&BinOp, const ASTContext *AstCtx) { + const auto *LhsBinOp = dyn_cast<BinaryOperator>(BinOp->getLHS()); + const auto *RhsBinOp = dyn_cast<BinaryOperator>(BinOp->getRHS()); + + if (!LhsBinOp || !RhsBinOp) + return false; + + auto IsIntegerConstantExpr = [AstCtx](const Expr *E) { + return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx); + }; + + if ((IsIntegerConstantExpr(LhsBinOp->getLHS()) || + IsIntegerConstantExpr(LhsBinOp->getRHS())) && + (IsIntegerConstantExpr(RhsBinOp->getLHS()) || + IsIntegerConstantExpr(RhsBinOp->getRHS()))) + return true; + return false; +} + +// Retrieves integer constant subexpressions from binary operator expressions +// that have two equivalent sides. +// E.g.: from (X == 5) && (X == 5) retrieves 5 and 5. +static bool retrieveConstExprFromBothSides(const BinaryOperator *&BinOp, + BinaryOperatorKind &MainOpcode, + BinaryOperatorKind &SideOpcode, + const Expr *&LhsConst, + const Expr *&RhsConst, + const ASTContext *AstCtx) { + assert(areSidesBinaryConstExpressions(BinOp, AstCtx) && + "Both sides of binary operator must be constant expressions!"); + + MainOpcode = BinOp->getOpcode(); + + const auto *BinOpLhs = cast<BinaryOperator>(BinOp->getLHS()); + const auto *BinOpRhs = cast<BinaryOperator>(BinOp->getRHS()); + + auto IsIntegerConstantExpr = [AstCtx](const Expr *E) { + return !E->isValueDependent() && E->isIntegerConstantExpr(*AstCtx); + }; + + LhsConst = IsIntegerConstantExpr(BinOpLhs->getLHS()) ? BinOpLhs->getLHS() + : BinOpLhs->getRHS(); + RhsConst = IsIntegerConstantExpr(BinOpRhs->getLHS()) ? BinOpRhs->getLHS() + : BinOpRhs->getRHS(); + + if (!LhsConst || !RhsConst) + return false; + + assert(BinOpLhs->getOpcode() == BinOpRhs->getOpcode() && + "Sides of the binary operator must be equivalent expressions!"); + + SideOpcode = BinOpLhs->getOpcode(); + + return true; +} + +static bool isSameRawIdentifierToken(const Token &T1, const Token &T2, + const SourceManager &SM) { + if (T1.getKind() != T2.getKind()) + return false; + if (T1.isNot(tok::raw_identifier)) + return true; + if (T1.getLength() != T2.getLength()) + return false; + return StringRef(SM.getCharacterData(T1.getLocation()), T1.getLength()) == + StringRef(SM.getCharacterData(T2.getLocation()), T2.getLength()); +} + +bool isTokAtEndOfExpr(SourceRange ExprSR, Token T, const SourceManager &SM) { + return SM.getExpansionLoc(ExprSR.getEnd()) == T.getLocation(); +} + +/// Returns true if both LhsExpr and RhsExpr are +/// macro expressions and they are expanded +/// from different macros. +static bool areExprsFromDifferentMacros(const Expr *LhsExpr, + const Expr *RhsExpr, + const ASTContext *AstCtx) { + if (!LhsExpr || !RhsExpr) + return false; + SourceRange Lsr = LhsExpr->getSourceRange(); + SourceRange Rsr = RhsExpr->getSourceRange(); + if (!Lsr.getBegin().isMacroID() || !Rsr.getBegin().isMacroID()) + return false; + + const SourceManager &SM = AstCtx->getSourceManager(); + const LangOptions &LO = AstCtx->getLangOpts(); + + std::pair<FileID, unsigned> LsrLocInfo = + SM.getDecomposedLoc(SM.getExpansionLoc(Lsr.getBegin())); + std::pair<FileID, unsigned> RsrLocInfo = + SM.getDecomposedLoc(SM.getExpansionLoc(Rsr.getBegin())); + llvm::MemoryBufferRef MB = SM.getBufferOrFake(LsrLocInfo.first); + + const char *LTokenPos = MB.getBufferStart() + LsrLocInfo.second; + const char *RTokenPos = MB.getBufferStart() + RsrLocInfo.second; + Lexer LRawLex(SM.getLocForStartOfFile(LsrLocInfo.first), LO, + MB.getBufferStart(), LTokenPos, MB.getBufferEnd()); + Lexer RRawLex(SM.getLocForStartOfFile(RsrLocInfo.first), LO, + MB.getBufferStart(), RTokenPos, MB.getBufferEnd()); + + Token LTok, RTok; + do { // Compare the expressions token-by-token. + LRawLex.LexFromRawLexer(LTok); + RRawLex.LexFromRawLexer(RTok); + } while (!LTok.is(tok::eof) && !RTok.is(tok::eof) && + isSameRawIdentifierToken(LTok, RTok, SM) && + !isTokAtEndOfExpr(Lsr, LTok, SM) && + !isTokAtEndOfExpr(Rsr, RTok, SM)); + return (!isTokAtEndOfExpr(Lsr, LTok, SM) || + !isTokAtEndOfExpr(Rsr, RTok, SM)) || + !isSameRawIdentifierToken(LTok, RTok, SM); +} + +static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr, + const Expr *&RhsExpr) { + if (!LhsExpr || !RhsExpr) + return false; + + SourceLocation LhsLoc = LhsExpr->getExprLoc(); + SourceLocation RhsLoc = RhsExpr->getExprLoc(); + + return LhsLoc.isMacroID() != RhsLoc.isMacroID(); +} +} // namespace + +void RedundantExpressionCheck::registerMatchers(MatchFinder *Finder) { + const auto AnyLiteralExpr = ignoringParenImpCasts( + anyOf(cxxBoolLiteral(), characterLiteral(), integerLiteral())); + + const auto BannedIntegerLiteral = + integerLiteral(expandedByMacro(KnownBannedMacroNames)); + const auto BannedAncestor = expr(isRequiresExpr()); + + // Binary with equivalent operands, like (X != 2 && X != 2). + Finder->addMatcher( + traverse(TK_AsIs, + binaryOperator( + anyOf(isComparisonOperator(), + hasAnyOperatorName("-", "/", "%", "|", "&", "^", "&&", + "||", "=")), + operandsAreEquivalent(), + // Filter noisy false positives. + unless(isInTemplateInstantiation()), + unless(binaryOperatorIsInMacro()), + unless(hasType(realFloatingPointType())), + unless(hasEitherOperand(hasType(realFloatingPointType()))), + unless(hasLHS(AnyLiteralExpr)), + unless(hasDescendant(BannedIntegerLiteral)), + unless(hasAncestor(BannedAncestor))) + .bind("binary")), + this); + + // Logical or bitwise operator with equivalent nested operands, like (X && Y + // && X) or (X && (Y && X)) + Finder->addMatcher( + binaryOperator(hasAnyOperatorName("|", "&", "||", "&&", "^"), + nestedOperandsAreEquivalent(), + // Filter noisy false positives. + unless(isInTemplateInstantiation()), + unless(binaryOperatorIsInMacro()), + // TODO: if the banned macros are themselves duplicated + unless(hasDescendant(BannedIntegerLiteral)), + unless(hasAncestor(BannedAncestor))) + .bind("nested-duplicates"), + this); + + // Conditional (ternary) operator with equivalent operands, like (Y ? X : X). + Finder->addMatcher( + traverse(TK_AsIs, + conditionalOperator(expressionsAreEquivalent(), + // Filter noisy false positives. + unless(conditionalOperatorIsInMacro()), + unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) + .bind("cond")), + this); + + // Overloaded operators with equivalent operands. + Finder->addMatcher( + traverse(TK_AsIs, + cxxOperatorCallExpr( + hasAnyOverloadedOperatorName("-", "/", "%", "|", "&", "^", + "==", "!=", "<", "<=", ">", + ">=", "&&", "||", "="), + parametersAreEquivalent(), + // Filter noisy false positives. + unless(isMacro()), unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) + .bind("call")), + this); + + // Overloaded operators with equivalent operands. + Finder->addMatcher( + cxxOperatorCallExpr( + hasAnyOverloadedOperatorName("|", "&", "||", "&&", "^"), + nestedParametersAreEquivalent(), argumentCountIs(2), + // Filter noisy false positives. + unless(isMacro()), unless(isInTemplateInstantiation()), + unless(hasAncestor(BannedAncestor))) + .bind("nested-duplicates"), + this); + + // Match expressions like: !(1 | 2 | 3) + Finder->addMatcher( + traverse(TK_AsIs, + implicitCastExpr( + hasImplicitDestinationType(isInteger()), + has(unaryOperator( + hasOperatorName("!"), + hasUnaryOperand(ignoringParenImpCasts(binaryOperator( + hasAnyOperatorName("|", "&"), + hasLHS(anyOf( + binaryOperator(hasAnyOperatorName("|", "&")), + integerLiteral())), + hasRHS(integerLiteral()))))) + .bind("logical-bitwise-confusion")), + unless(hasAncestor(BannedAncestor)))), + this); + + // Match expressions like: (X << 8) & 0xFF + Finder->addMatcher( + traverse(TK_AsIs, + binaryOperator( + hasOperatorName("&"), + hasOperands(ignoringParenImpCasts(binaryOperator( + hasOperatorName("<<"), + hasRHS(ignoringParenImpCasts( + integerLiteral().bind("shift-const"))))), + ignoringParenImpCasts( + integerLiteral().bind("and-const"))), + unless(hasAncestor(BannedAncestor))) + .bind("left-right-shift-confusion")), + this); + + // Match common expressions and apply more checks to find redundant + // sub-expressions. + // a) Expr <op> K1 == K2 + // b) Expr <op> K1 == Expr + // c) Expr <op> K1 == Expr <op> K2 + // see: 'checkArithmeticExpr' and 'checkBitwiseExpr' + const auto BinOpCstLeft = matchBinOpIntegerConstantExpr("lhs"); + const auto BinOpCstRight = matchBinOpIntegerConstantExpr("rhs"); + const auto CstRight = matchIntegerConstantExpr("rhs"); + const auto SymRight = matchSymbolicExpr("rhs"); + + // Match expressions like: x <op> 0xFF == 0xF00. + Finder->addMatcher( + traverse(TK_AsIs, binaryOperator(isComparisonOperator(), + hasOperands(BinOpCstLeft, CstRight), + unless(hasAncestor(BannedAncestor))) + .bind("binop-const-compare-to-const")), + this); + + // Match expressions like: x <op> 0xFF == x. + Finder->addMatcher( + traverse( + TK_AsIs, + binaryOperator(isComparisonOperator(), + anyOf(allOf(hasLHS(BinOpCstLeft), hasRHS(SymRight)), + allOf(hasLHS(SymRight), hasRHS(BinOpCstLeft))), + unless(hasAncestor(BannedAncestor))) + .bind("binop-const-compare-to-sym")), + this); + + // Match expressions like: x <op> 10 == x <op> 12. + Finder->addMatcher( + traverse(TK_AsIs, + binaryOperator(isComparisonOperator(), hasLHS(BinOpCstLeft), + hasRHS(BinOpCstRight), + // Already reported as redundant. + unless(operandsAreEquivalent()), + unless(hasAncestor(BannedAncestor))) + .bind("binop-const-compare-to-binop-const")), + this); + + // Match relational expressions combined with logical operators and find + // redundant sub-expressions. + // see: 'checkRelationalExpr' + + // Match expressions like: x < 2 && x > 2. + const auto ComparisonLeft = matchRelationalIntegerConstantExpr("lhs"); + const auto ComparisonRight = matchRelationalIntegerConstantExpr("rhs"); + Finder->addMatcher( + traverse(TK_AsIs, + binaryOperator(hasAnyOperatorName("||", "&&"), + hasLHS(ComparisonLeft), hasRHS(ComparisonRight), + // Already reported as redundant. + unless(operandsAreEquivalent()), + unless(hasAncestor(BannedAncestor))) + .bind("comparisons-of-symbol-and-const")), + this); +} + +void RedundantExpressionCheck::checkArithmeticExpr( + const MatchFinder::MatchResult &Result) { + APSInt LhsValue, RhsValue; + const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr; + BinaryOperatorKind LhsOpcode, RhsOpcode; + + if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>( + "binop-const-compare-to-sym")) { + BinaryOperatorKind Opcode = ComparisonOperator->getOpcode(); + if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol, + LhsValue) || + !retrieveSymbolicExpr(Result, "rhs", RhsSymbol) || + !areEquivalentExpr(LhsSymbol, RhsSymbol)) + return; + + // Check expressions: x + k == x or x - k == x. + if (LhsOpcode == BO_Add || LhsOpcode == BO_Sub) { + if ((LhsValue != 0 && Opcode == BO_EQ) || + (LhsValue == 0 && Opcode == BO_NE)) + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always false"); + else if ((LhsValue == 0 && Opcode == BO_EQ) || + (LhsValue != 0 && Opcode == BO_NE)) + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always true"); + } + } else if (const auto *ComparisonOperator = + Result.Nodes.getNodeAs<BinaryOperator>( + "binop-const-compare-to-binop-const")) { + BinaryOperatorKind Opcode = ComparisonOperator->getOpcode(); + + if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol, + LhsValue) || + !retrieveBinOpIntegerConstantExpr(Result, "rhs", RhsOpcode, RhsSymbol, + RhsValue) || + !areEquivalentExpr(LhsSymbol, RhsSymbol)) + return; + + transformSubToCanonicalAddExpr(LhsOpcode, LhsValue); + transformSubToCanonicalAddExpr(RhsOpcode, RhsValue); + + // Check expressions: x + 1 == x + 2 or x + 1 != x + 2. + if (LhsOpcode == BO_Add && RhsOpcode == BO_Add) { + if ((Opcode == BO_EQ && APSInt::compareValues(LhsValue, RhsValue) == 0) || + (Opcode == BO_NE && APSInt::compareValues(LhsValue, RhsValue) != 0)) { + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always true"); + } else if ((Opcode == BO_EQ && + APSInt::compareValues(LhsValue, RhsValue) != 0) || + (Opcode == BO_NE && + APSInt::compareValues(LhsValue, RhsValue) == 0)) { + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always false"); + } + } + } +} + +static bool exprEvaluatesToZero(BinaryOperatorKind Opcode, APSInt Value) { + return (Opcode == BO_And || Opcode == BO_AndAssign) && Value == 0; +} + +static bool exprEvaluatesToBitwiseNegatedZero(BinaryOperatorKind Opcode, + APSInt Value) { + return (Opcode == BO_Or || Opcode == BO_OrAssign) && ~Value == 0; +} + +static bool exprEvaluatesToSymbolic(BinaryOperatorKind Opcode, APSInt Value) { + return ((Opcode == BO_Or || Opcode == BO_OrAssign) && Value == 0) || + ((Opcode == BO_And || Opcode == BO_AndAssign) && ~Value == 0); +} + + +void RedundantExpressionCheck::checkBitwiseExpr( + const MatchFinder::MatchResult &Result) { + if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>( + "binop-const-compare-to-const")) { + BinaryOperatorKind Opcode = ComparisonOperator->getOpcode(); + + APSInt LhsValue, RhsValue; + const Expr *LhsSymbol = nullptr; + BinaryOperatorKind LhsOpcode; + if (!retrieveBinOpIntegerConstantExpr(Result, "lhs", LhsOpcode, LhsSymbol, + LhsValue) || + !retrieveIntegerConstantExpr(Result, "rhs", RhsValue)) + return; + + uint64_t LhsConstant = LhsValue.getZExtValue(); + uint64_t RhsConstant = RhsValue.getZExtValue(); + SourceLocation Loc = ComparisonOperator->getOperatorLoc(); + + // Check expression: x & k1 == k2 (i.e. x & 0xFF == 0xF00) + if (LhsOpcode == BO_And && (LhsConstant & RhsConstant) != RhsConstant) { + if (Opcode == BO_EQ) + diag(Loc, "logical expression is always false"); + else if (Opcode == BO_NE) + diag(Loc, "logical expression is always true"); + } + + // Check expression: x | k1 == k2 (i.e. x | 0xFF == 0xF00) + if (LhsOpcode == BO_Or && (LhsConstant | RhsConstant) != RhsConstant) { + if (Opcode == BO_EQ) + diag(Loc, "logical expression is always false"); + else if (Opcode == BO_NE) + diag(Loc, "logical expression is always true"); + } + } else if (const auto *IneffectiveOperator = + Result.Nodes.getNodeAs<BinaryOperator>( + "ineffective-bitwise")) { + APSInt Value; + const Expr *Sym = nullptr, *ConstExpr = nullptr; + + if (!retrieveSymbolicExpr(Result, "ineffective-bitwise", Sym) || + !retrieveIntegerConstantExpr(Result, "ineffective-bitwise", Value, + ConstExpr)) + return; + + if((Value != 0 && ~Value != 0) || Sym->getExprLoc().isMacroID()) + return; + + SourceLocation Loc = IneffectiveOperator->getOperatorLoc(); + + BinaryOperatorKind Opcode = IneffectiveOperator->getOpcode(); + if (exprEvaluatesToZero(Opcode, Value)) { + diag(Loc, "expression always evaluates to 0"); + } else if (exprEvaluatesToBitwiseNegatedZero(Opcode, Value)) { + SourceRange ConstExprRange(ConstExpr->getBeginLoc(), + ConstExpr->getEndLoc()); + StringRef ConstExprText = Lexer::getSourceText( + CharSourceRange::getTokenRange(ConstExprRange), *Result.SourceManager, + Result.Context->getLangOpts()); + + diag(Loc, "expression always evaluates to '%0'") << ConstExprText; + + } else if (exprEvaluatesToSymbolic(Opcode, Value)) { + SourceRange SymExprRange(Sym->getBeginLoc(), Sym->getEndLoc()); + + StringRef ExprText = Lexer::getSourceText( + CharSourceRange::getTokenRange(SymExprRange), *Result.SourceManager, + Result.Context->getLangOpts()); + + diag(Loc, "expression always evaluates to '%0'") << ExprText; + } + } +} + +void RedundantExpressionCheck::checkRelationalExpr( + const MatchFinder::MatchResult &Result) { + if (const auto *ComparisonOperator = Result.Nodes.getNodeAs<BinaryOperator>( + "comparisons-of-symbol-and-const")) { + // Matched expressions are: (x <op> k1) <REL> (x <op> k2). + // E.g.: (X < 2) && (X > 4) + BinaryOperatorKind Opcode = ComparisonOperator->getOpcode(); + + const Expr *LhsExpr = nullptr, *RhsExpr = nullptr; + const Expr *LhsSymbol = nullptr, *RhsSymbol = nullptr; + const Expr *LhsConst = nullptr, *RhsConst = nullptr; + BinaryOperatorKind LhsOpcode, RhsOpcode; + APSInt LhsValue, RhsValue; + + if (!retrieveRelationalIntegerConstantExpr( + Result, "lhs", LhsExpr, LhsOpcode, LhsSymbol, LhsValue, LhsConst) || + !retrieveRelationalIntegerConstantExpr( + Result, "rhs", RhsExpr, RhsOpcode, RhsSymbol, RhsValue, RhsConst) || + !areEquivalentExpr(LhsSymbol, RhsSymbol)) + return; + + // Bring expr to a canonical form: smallest constant must be on the left. + if (APSInt::compareValues(LhsValue, RhsValue) > 0) { + std::swap(LhsExpr, RhsExpr); + std::swap(LhsValue, RhsValue); + std::swap(LhsSymbol, RhsSymbol); + std::swap(LhsOpcode, RhsOpcode); + } + + // Constants come from two different macros, or one of them is a macro. + if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) || + areExprsMacroAndNonMacro(LhsConst, RhsConst)) + return; + + if ((Opcode == BO_LAnd || Opcode == BO_LOr) && + areEquivalentRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) { + diag(ComparisonOperator->getOperatorLoc(), + "equivalent expression on both sides of logical operator"); + return; + } + + if (Opcode == BO_LAnd) { + if (areExclusiveRanges(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) { + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always false"); + } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) { + diag(LhsExpr->getExprLoc(), "expression is redundant"); + } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) { + diag(RhsExpr->getExprLoc(), "expression is redundant"); + } + } + + if (Opcode == BO_LOr) { + if (rangesFullyCoverDomain(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) { + diag(ComparisonOperator->getOperatorLoc(), + "logical expression is always true"); + } else if (rangeSubsumesRange(LhsOpcode, LhsValue, RhsOpcode, RhsValue)) { + diag(RhsExpr->getExprLoc(), "expression is redundant"); + } else if (rangeSubsumesRange(RhsOpcode, RhsValue, LhsOpcode, LhsValue)) { + diag(LhsExpr->getExprLoc(), "expression is redundant"); + } + } + } +} + +void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binary")) { + // If the expression's constants are macros, check whether they are + // intentional. + if (areSidesBinaryConstExpressions(BinOp, Result.Context)) { + const Expr *LhsConst = nullptr, *RhsConst = nullptr; + BinaryOperatorKind MainOpcode, SideOpcode; + + if (!retrieveConstExprFromBothSides(BinOp, MainOpcode, SideOpcode, + LhsConst, RhsConst, Result.Context)) + return; + + if (areExprsFromDifferentMacros(LhsConst, RhsConst, Result.Context) || + areExprsMacroAndNonMacro(LhsConst, RhsConst)) + return; + } + + diag(BinOp->getOperatorLoc(), "both sides of operator are equivalent"); + } + + if (const auto *CondOp = + Result.Nodes.getNodeAs<ConditionalOperator>("cond")) { + const Expr *TrueExpr = CondOp->getTrueExpr(); + const Expr *FalseExpr = CondOp->getFalseExpr(); + + if (areExprsFromDifferentMacros(TrueExpr, FalseExpr, Result.Context) || + areExprsMacroAndNonMacro(TrueExpr, FalseExpr)) + return; + diag(CondOp->getColonLoc(), + "'true' and 'false' expressions are equivalent"); + } + + if (const auto *Call = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("call")) { + if (canOverloadedOperatorArgsBeModified(Call, true)) + return; + + diag(Call->getOperatorLoc(), + "both sides of overloaded operator are equivalent"); + } + + if (const auto *Op = Result.Nodes.getNodeAs<Expr>("nested-duplicates")) { + const auto *Call = dyn_cast<CXXOperatorCallExpr>(Op); + if (Call && canOverloadedOperatorArgsBeModified(Call, true)) + return; + + StringRef Message = + Call ? "overloaded operator has equivalent nested operands" + : "operator has equivalent nested operands"; + + const auto Diag = diag(Op->getExprLoc(), Message); + for (const auto &KeyValue : Result.Nodes.getMap()) { + if (StringRef(KeyValue.first).startswith("duplicate")) + Diag << KeyValue.second.getSourceRange(); + } + } + + if (const auto *NegateOperator = + Result.Nodes.getNodeAs<UnaryOperator>("logical-bitwise-confusion")) { + SourceLocation OperatorLoc = NegateOperator->getOperatorLoc(); + + auto Diag = + diag(OperatorLoc, + "ineffective logical negation operator used; did you mean '~'?"); + SourceLocation LogicalNotLocation = OperatorLoc.getLocWithOffset(1); + + if (!LogicalNotLocation.isMacroID()) + Diag << FixItHint::CreateReplacement( + CharSourceRange::getCharRange(OperatorLoc, LogicalNotLocation), "~"); + } + + if (const auto *BinaryAndExpr = Result.Nodes.getNodeAs<BinaryOperator>( + "left-right-shift-confusion")) { + const auto *ShiftingConst = Result.Nodes.getNodeAs<Expr>("shift-const"); + assert(ShiftingConst && "Expr* 'ShiftingConst' is nullptr!"); + std::optional<llvm::APSInt> ShiftingValue = + ShiftingConst->getIntegerConstantExpr(*Result.Context); + + if (!ShiftingValue) + return; + + const auto *AndConst = Result.Nodes.getNodeAs<Expr>("and-const"); + assert(AndConst && "Expr* 'AndCont' is nullptr!"); + std::optional<llvm::APSInt> AndValue = + AndConst->getIntegerConstantExpr(*Result.Context); + if (!AndValue) + return; + + // If ShiftingConst is shifted left with more bits than the position of the + // leftmost 1 in the bit representation of AndValue, AndConstant is + // ineffective. + if (AndValue->getActiveBits() > *ShiftingValue) + return; + + auto Diag = diag(BinaryAndExpr->getOperatorLoc(), + "ineffective bitwise and operation"); + } + + // Check for the following bound expressions: + // - "binop-const-compare-to-sym", + // - "binop-const-compare-to-binop-const", + // Produced message: + // -> "logical expression is always false/true" + checkArithmeticExpr(Result); + + // Check for the following bound expression: + // - "binop-const-compare-to-const", + // - "ineffective-bitwise" + // Produced message: + // -> "logical expression is always false/true" + // -> "expression always evaluates to ..." + checkBitwiseExpr(Result); + + // Check for te following bound expression: + // - "comparisons-of-symbol-and-const", + // Produced messages: + // -> "equivalent expression on both sides of logical operator", + // -> "logical expression is always false/true" + // -> "expression is redundant" + checkRelationalExpr(Result); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.h new file mode 100644 index 0000000000..7b3b84b5b3 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/RedundantExpressionCheck.h @@ -0,0 +1,36 @@ +//===--- RedundantExpressionCheck.h - clang-tidy-----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_REDUNDANT_EXPRESSION_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_REDUNDANT_EXPRESSION_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// The checker detects expressions that are redundant, because they contain +/// ineffective, useless parts. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/redundant-expression.html +class RedundantExpressionCheck : public ClangTidyCheck { +public: + RedundantExpressionCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + void checkArithmeticExpr(const ast_matchers::MatchFinder::MatchResult &R); + void checkBitwiseExpr(const ast_matchers::MatchFinder::MatchResult &R); + void checkRelationalExpr(const ast_matchers::MatchFinder::MatchResult &R); +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_REDUNDANT_EXPRESSION_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.cpp new file mode 100644 index 0000000000..a2fba6760a --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.cpp @@ -0,0 +1,168 @@ +//===--- StaticAssertCheck.cpp - clang-tidy -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "StaticAssertCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Casting.h" +#include <optional> +#include <string> + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +StaticAssertCheck::StaticAssertCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + +void StaticAssertCheck::registerMatchers(MatchFinder *Finder) { + auto NegatedString = unaryOperator( + hasOperatorName("!"), hasUnaryOperand(ignoringImpCasts(stringLiteral()))); + auto IsAlwaysFalse = + expr(anyOf(cxxBoolLiteral(equals(false)), integerLiteral(equals(0)), + cxxNullPtrLiteralExpr(), gnuNullExpr(), NegatedString)) + .bind("isAlwaysFalse"); + auto IsAlwaysFalseWithCast = ignoringParenImpCasts(anyOf( + IsAlwaysFalse, cStyleCastExpr(has(ignoringParenImpCasts(IsAlwaysFalse))) + .bind("castExpr"))); + auto AssertExprRoot = anyOf( + binaryOperator( + hasAnyOperatorName("&&", "=="), + hasEitherOperand(ignoringImpCasts(stringLiteral().bind("assertMSG"))), + anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)), + anything())) + .bind("assertExprRoot"), + IsAlwaysFalse); + auto NonConstexprFunctionCall = + callExpr(hasDeclaration(functionDecl(unless(isConstexpr())))); + auto AssertCondition = + expr( + anyOf(expr(ignoringParenCasts(anyOf( + AssertExprRoot, unaryOperator(hasUnaryOperand( + ignoringParenCasts(AssertExprRoot)))))), + anything()), + unless(findAll(NonConstexprFunctionCall))) + .bind("condition"); + auto Condition = + anyOf(ignoringParenImpCasts(callExpr( + hasDeclaration(functionDecl(hasName("__builtin_expect"))), + hasArgument(0, AssertCondition))), + AssertCondition); + + Finder->addMatcher(conditionalOperator(hasCondition(Condition), + unless(isInTemplateInstantiation())) + .bind("condStmt"), + this); + + Finder->addMatcher( + ifStmt(hasCondition(Condition), unless(isInTemplateInstantiation())) + .bind("condStmt"), + this); +} + +void StaticAssertCheck::check(const MatchFinder::MatchResult &Result) { + const ASTContext *ASTCtx = Result.Context; + const LangOptions &Opts = ASTCtx->getLangOpts(); + const SourceManager &SM = ASTCtx->getSourceManager(); + const auto *CondStmt = Result.Nodes.getNodeAs<Stmt>("condStmt"); + const auto *Condition = Result.Nodes.getNodeAs<Expr>("condition"); + const auto *IsAlwaysFalse = Result.Nodes.getNodeAs<Expr>("isAlwaysFalse"); + const auto *AssertMSG = Result.Nodes.getNodeAs<StringLiteral>("assertMSG"); + const auto *AssertExprRoot = + Result.Nodes.getNodeAs<BinaryOperator>("assertExprRoot"); + const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("castExpr"); + SourceLocation AssertExpansionLoc = CondStmt->getBeginLoc(); + + if (!AssertExpansionLoc.isValid() || !AssertExpansionLoc.isMacroID()) + return; + + StringRef MacroName = + Lexer::getImmediateMacroName(AssertExpansionLoc, SM, Opts); + + if (MacroName != "assert" || Condition->isValueDependent() || + Condition->isTypeDependent() || Condition->isInstantiationDependent() || + !Condition->isEvaluatable(*ASTCtx)) + return; + + // False literal is not the result of macro expansion. + if (IsAlwaysFalse && (!CastExpr || CastExpr->getType()->isPointerType())) { + SourceLocation FalseLiteralLoc = + SM.getImmediateSpellingLoc(IsAlwaysFalse->getExprLoc()); + if (!FalseLiteralLoc.isMacroID()) + return; + + StringRef FalseMacroName = + Lexer::getImmediateMacroName(FalseLiteralLoc, SM, Opts); + if (FalseMacroName.compare_insensitive("false") == 0 || + FalseMacroName.compare_insensitive("null") == 0) + return; + } + + SourceLocation AssertLoc = SM.getImmediateMacroCallerLoc(AssertExpansionLoc); + + SmallVector<FixItHint, 4> FixItHints; + SourceLocation LastParenLoc; + if (AssertLoc.isValid() && !AssertLoc.isMacroID() && + (LastParenLoc = getLastParenLoc(ASTCtx, AssertLoc)).isValid()) { + FixItHints.push_back( + FixItHint::CreateReplacement(SourceRange(AssertLoc), "static_assert")); + + if (AssertExprRoot) { + FixItHints.push_back(FixItHint::CreateRemoval( + SourceRange(AssertExprRoot->getOperatorLoc()))); + FixItHints.push_back(FixItHint::CreateRemoval( + SourceRange(AssertMSG->getBeginLoc(), AssertMSG->getEndLoc()))); + FixItHints.push_back(FixItHint::CreateInsertion( + LastParenLoc, (Twine(", \"") + AssertMSG->getString() + "\"").str())); + } else if (!Opts.CPlusPlus17) { + FixItHints.push_back(FixItHint::CreateInsertion(LastParenLoc, ", \"\"")); + } + } + + diag(AssertLoc, "found assert() that could be replaced by static_assert()") + << FixItHints; +} + +SourceLocation StaticAssertCheck::getLastParenLoc(const ASTContext *ASTCtx, + SourceLocation AssertLoc) { + const LangOptions &Opts = ASTCtx->getLangOpts(); + const SourceManager &SM = ASTCtx->getSourceManager(); + + std::optional<llvm::MemoryBufferRef> Buffer = + SM.getBufferOrNone(SM.getFileID(AssertLoc)); + if (!Buffer) + return SourceLocation(); + + const char *BufferPos = SM.getCharacterData(AssertLoc); + + Token Token; + Lexer Lexer(SM.getLocForStartOfFile(SM.getFileID(AssertLoc)), Opts, + Buffer->getBufferStart(), BufferPos, Buffer->getBufferEnd()); + + // assert first left parenthesis + if (Lexer.LexFromRawLexer(Token) || Lexer.LexFromRawLexer(Token) || + !Token.is(tok::l_paren)) + return SourceLocation(); + + unsigned int ParenCount = 1; + while (ParenCount && !Lexer.LexFromRawLexer(Token)) { + if (Token.is(tok::l_paren)) + ++ParenCount; + else if (Token.is(tok::r_paren)) + --ParenCount; + } + + return Token.getLocation(); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.h new file mode 100644 index 0000000000..7b378e0164 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/StaticAssertCheck.h @@ -0,0 +1,39 @@ +//===--- StaticAssertCheck.h - clang-tidy -----------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_STATICASSERTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_STATICASSERTCHECK_H + +#include "../ClangTidyCheck.h" +#include "llvm/ADT/StringRef.h" +#include <string> + +namespace clang::tidy::misc { + +/// Replaces `assert()` with `static_assert()` if the condition is evaluatable +/// at compile time. +/// +/// The condition of `static_assert()` is evaluated at compile time which is +/// safer and more efficient. +class StaticAssertCheck : public ClangTidyCheck { +public: + StaticAssertCheck(StringRef Name, ClangTidyContext *Context); + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus11 || LangOpts.C11; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + SourceLocation getLastParenLoc(const ASTContext *ASTCtx, + SourceLocation AssertLoc); +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_STATICASSERTCHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp new file mode 100644 index 0000000000..abe7bed9df --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp @@ -0,0 +1,171 @@ +//===--- ThrowByValueCatchByReferenceCheck.cpp - clang-tidy----------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ThrowByValueCatchByReferenceCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/OperationKinds.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +ThrowByValueCatchByReferenceCheck::ThrowByValueCatchByReferenceCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + CheckAnonymousTemporaries(Options.get("CheckThrowTemporaries", true)), + WarnOnLargeObject(Options.get("WarnOnLargeObject", false)), + // Cannot access `ASTContext` from here so set it to an extremal value. + MaxSizeOptions( + Options.get("MaxSize", std::numeric_limits<uint64_t>::max())), + MaxSize(MaxSizeOptions) {} + +void ThrowByValueCatchByReferenceCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(cxxThrowExpr().bind("throw"), this); + Finder->addMatcher(cxxCatchStmt().bind("catch"), this); +} + +void ThrowByValueCatchByReferenceCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "CheckThrowTemporaries", true); + Options.store(Opts, "WarnOnLargeObjects", WarnOnLargeObject); + Options.store(Opts, "MaxSize", MaxSizeOptions); +} + +void ThrowByValueCatchByReferenceCheck::check( + const MatchFinder::MatchResult &Result) { + diagnoseThrowLocations(Result.Nodes.getNodeAs<CXXThrowExpr>("throw")); + diagnoseCatchLocations(Result.Nodes.getNodeAs<CXXCatchStmt>("catch"), + *Result.Context); +} + +bool ThrowByValueCatchByReferenceCheck::isFunctionParameter( + const DeclRefExpr *DeclRefExpr) { + return isa<ParmVarDecl>(DeclRefExpr->getDecl()); +} + +bool ThrowByValueCatchByReferenceCheck::isCatchVariable( + const DeclRefExpr *DeclRefExpr) { + auto *ValueDecl = DeclRefExpr->getDecl(); + if (auto *VarDecl = dyn_cast<clang::VarDecl>(ValueDecl)) + return VarDecl->isExceptionVariable(); + return false; +} + +bool ThrowByValueCatchByReferenceCheck::isFunctionOrCatchVar( + const DeclRefExpr *DeclRefExpr) { + return isFunctionParameter(DeclRefExpr) || isCatchVariable(DeclRefExpr); +} + +void ThrowByValueCatchByReferenceCheck::diagnoseThrowLocations( + const CXXThrowExpr *ThrowExpr) { + if (!ThrowExpr) + return; + auto *SubExpr = ThrowExpr->getSubExpr(); + if (!SubExpr) + return; + auto QualType = SubExpr->getType(); + if (QualType->isPointerType()) { + // The code is throwing a pointer. + // In case it is string literal, it is safe and we return. + auto *Inner = SubExpr->IgnoreParenImpCasts(); + if (isa<StringLiteral>(Inner)) + return; + // If it's a variable from a catch statement, we return as well. + auto *DeclRef = dyn_cast<DeclRefExpr>(Inner); + if (DeclRef && isCatchVariable(DeclRef)) { + return; + } + diag(SubExpr->getBeginLoc(), "throw expression throws a pointer; it should " + "throw a non-pointer value instead"); + } + // If the throw statement does not throw by pointer then it throws by value + // which is ok. + // There are addition checks that emit diagnosis messages if the thrown value + // is not an RValue. See: + // https://www.securecoding.cert.org/confluence/display/cplusplus/ERR09-CPP.+Throw+anonymous+temporaries + // This behavior can be influenced by an option. + + // If we encounter a CXXThrowExpr, we move through all casts until you either + // encounter a DeclRefExpr or a CXXConstructExpr. + // If it's a DeclRefExpr, we emit a message if the referenced variable is not + // a catch variable or function parameter. + // When encountering a CopyOrMoveConstructor: emit message if after casts, + // the expression is a LValue + if (CheckAnonymousTemporaries) { + bool Emit = false; + auto *CurrentSubExpr = SubExpr->IgnoreImpCasts(); + const auto *VariableReference = dyn_cast<DeclRefExpr>(CurrentSubExpr); + const auto *ConstructorCall = dyn_cast<CXXConstructExpr>(CurrentSubExpr); + // If we have a DeclRefExpr, we flag for emitting a diagnosis message in + // case the referenced variable is neither a function parameter nor a + // variable declared in the catch statement. + if (VariableReference) + Emit = !isFunctionOrCatchVar(VariableReference); + else if (ConstructorCall && + ConstructorCall->getConstructor()->isCopyOrMoveConstructor()) { + // If we have a copy / move construction, we emit a diagnosis message if + // the object that we copy construct from is neither a function parameter + // nor a variable declared in a catch statement + auto ArgIter = + ConstructorCall + ->arg_begin(); // there's only one for copy constructors + auto *CurrentSubExpr = (*ArgIter)->IgnoreImpCasts(); + if (CurrentSubExpr->isLValue()) { + if (auto *Tmp = dyn_cast<DeclRefExpr>(CurrentSubExpr)) + Emit = !isFunctionOrCatchVar(Tmp); + else if (isa<CallExpr>(CurrentSubExpr)) + Emit = true; + } + } + if (Emit) + diag(SubExpr->getBeginLoc(), + "throw expression should throw anonymous temporary values instead"); + } +} + +void ThrowByValueCatchByReferenceCheck::diagnoseCatchLocations( + const CXXCatchStmt *CatchStmt, ASTContext &Context) { + if (!CatchStmt) + return; + auto CaughtType = CatchStmt->getCaughtType(); + if (CaughtType.isNull()) + return; + auto *VarDecl = CatchStmt->getExceptionDecl(); + if (const auto *PT = CaughtType.getCanonicalType()->getAs<PointerType>()) { + const char *DiagMsgCatchReference = + "catch handler catches a pointer value; " + "should throw a non-pointer value and " + "catch by reference instead"; + // We do not diagnose when catching pointer to strings since we also allow + // throwing string literals. + if (!PT->getPointeeType()->isAnyCharacterType()) + diag(VarDecl->getBeginLoc(), DiagMsgCatchReference); + } else if (!CaughtType->isReferenceType()) { + const char *DiagMsgCatchReference = "catch handler catches by value; " + "should catch by reference instead"; + // If it's not a pointer and not a reference then it must be caught "by + // value". In this case we should emit a diagnosis message unless the type + // is trivial. + if (!CaughtType.isTrivialType(Context)) { + diag(VarDecl->getBeginLoc(), DiagMsgCatchReference); + } else if (WarnOnLargeObject) { + // If the type is trivial, then catching it by reference is not dangerous. + // However, catching large objects by value decreases the performance. + + // We can now access `ASTContext` so if `MaxSize` is an extremal value + // then set it to the size of `size_t`. + if (MaxSize == std::numeric_limits<uint64_t>::max()) + MaxSize = Context.getTypeSize(Context.getSizeType()); + if (Context.getTypeSize(CaughtType) > MaxSize) + diag(VarDecl->getBeginLoc(), DiagMsgCatchReference); + } + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h new file mode 100644 index 0000000000..0d4df97c7b --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h @@ -0,0 +1,52 @@ +//===--- ThrowByValueCatchByReferenceCheck.h - clang-tidy--------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +///checks for locations that do not throw by value +// or catch by reference. +// The check is C++ only. It checks that all throw locations +// throw by value and not by pointer. Additionally it +// contains an option ("CheckThrowTemporaries", default value "true") that +// checks that thrown objects are anonymous temporaries. It is also +// acceptable for this check to throw string literals. +// This test checks that exceptions are caught by reference +// and not by value or pointer. It will not warn when catching +// pointer to char, wchar_t, char16_t or char32_t. This is +// due to not warning on throwing string literals. +class ThrowByValueCatchByReferenceCheck : public ClangTidyCheck { +public: + ThrowByValueCatchByReferenceCheck(StringRef Name, ClangTidyContext *Context); + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + void diagnoseThrowLocations(const CXXThrowExpr *ThrowExpr); + void diagnoseCatchLocations(const CXXCatchStmt *CatchStmt, + ASTContext &Context); + bool isFunctionParameter(const DeclRefExpr *DeclRefExpr); + bool isCatchVariable(const DeclRefExpr *DeclRefExpr); + bool isFunctionOrCatchVar(const DeclRefExpr *DeclRefExpr); + const bool CheckAnonymousTemporaries; + const bool WarnOnLargeObject; + const uint64_t MaxSizeOptions; // The raw value read from the options. + uint64_t MaxSize; // No `const` because we have to set it in two steps. +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_THROW_BY_VALUE_CATCH_BY_REFERENCE_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp new file mode 100644 index 0000000000..42c4b6edb6 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp @@ -0,0 +1,90 @@ +//===--- UnconventionalAssignOperatorCheck.cpp - clang-tidy -----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UnconventionalAssignOperatorCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void UnconventionalAssignOperatorCheck::registerMatchers( + ast_matchers::MatchFinder *Finder) { + const auto HasGoodReturnType = + cxxMethodDecl(returns(hasCanonicalType(lValueReferenceType(pointee( + unless(isConstQualified()), + anyOf(autoType(), hasDeclaration(equalsBoundNode("class")))))))); + + const auto IsSelf = qualType(hasCanonicalType( + anyOf(hasDeclaration(equalsBoundNode("class")), + referenceType(pointee(hasDeclaration(equalsBoundNode("class"))))))); + const auto IsAssign = + cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())), + hasName("operator="), ofClass(recordDecl().bind("class"))) + .bind("method"); + const auto IsSelfAssign = + cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf)))) + .bind("method"); + + Finder->addMatcher( + cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind("ReturnType"), + this); + + const auto BadSelf = qualType(hasCanonicalType(referenceType( + anyOf(lValueReferenceType(pointee(unless(isConstQualified()))), + rValueReferenceType(pointee(isConstQualified())))))); + + Finder->addMatcher( + cxxMethodDecl(IsSelfAssign, + hasParameter(0, parmVarDecl(hasType(BadSelf)))) + .bind("ArgumentType"), + this); + + Finder->addMatcher( + cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind("cv"), + this); + + const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts( + anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())), + cxxOperatorCallExpr(argumentCountIs(1), + callee(unresolvedLookupExpr()), + hasArgument(0, cxxThisExpr())), + cxxOperatorCallExpr( + hasOverloadedOperatorName("="), + hasArgument( + 0, unaryOperator(hasOperatorName("*"), + hasUnaryOperand(cxxThisExpr()))))))))); + const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType); + + Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign)) + .bind("returnStmt"), + this); +} + +void UnconventionalAssignOperatorCheck::check( + const MatchFinder::MatchResult &Result) { + if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) { + diag(RetStmt->getBeginLoc(), "operator=() should always return '*this'"); + } else { + const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method"); + if (Result.Nodes.getNodeAs<CXXMethodDecl>("ReturnType")) + diag(Method->getBeginLoc(), "operator=() should return '%0&'") + << Method->getParent()->getName(); + if (Result.Nodes.getNodeAs<CXXMethodDecl>("ArgumentType")) + diag(Method->getBeginLoc(), + "operator=() should take '%0 const&'%select{|, '%0&&'}1 or '%0'") + << Method->getParent()->getName() << getLangOpts().CPlusPlus11; + if (Result.Nodes.getNodeAs<CXXMethodDecl>("cv")) + diag(Method->getBeginLoc(), + "operator=() should not be marked '%select{const|virtual}0'") + << !Method->isConst(); + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.h new file mode 100644 index 0000000000..c1aefaa879 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.h @@ -0,0 +1,40 @@ +//===--- UnconventionalAssignOperatorCheck.h - clang-tidy -------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGNOPERATORSIGNATURECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGNOPERATORSIGNATURECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// Finds declarations of assignment operators with the wrong return and/or +/// argument types and definitions with good return type but wrong return +/// statements. +/// +/// * The return type must be `Class&`. +/// * Works with move-assign and assign by value. +/// * Private and deleted operators are ignored. +/// * The operator must always return ``*this``. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/unconventional-assign-operator.html +class UnconventionalAssignOperatorCheck : public ClangTidyCheck { +public: + UnconventionalAssignOperatorCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_ASSIGNOPERATORSIGNATURECHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp new file mode 100644 index 0000000000..0e24b47f50 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp @@ -0,0 +1,149 @@ +//===--- UniqueptrResetReleaseCheck.cpp - clang-tidy ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UniqueptrResetReleaseCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +UniqueptrResetReleaseCheck::UniqueptrResetReleaseCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + Inserter(Options.getLocalOrGlobal("IncludeStyle", + utils::IncludeSorter::IS_LLVM), + areDiagsSelfContained()) {} + +void UniqueptrResetReleaseCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IncludeStyle", Inserter.getStyle()); +} + +void UniqueptrResetReleaseCheck::registerPPCallbacks( + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { + Inserter.registerPreprocessor(PP); +} + +void UniqueptrResetReleaseCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + cxxMemberCallExpr( + callee(memberExpr( + member(cxxMethodDecl( + hasName("reset"), + ofClass(cxxRecordDecl(hasName("::std::unique_ptr"), + decl().bind("left_class")))))) + .bind("reset_member")), + hasArgument( + 0, ignoringParenImpCasts(cxxMemberCallExpr( + on(expr().bind("right")), + callee(memberExpr(member(cxxMethodDecl( + hasName("release"), + ofClass(cxxRecordDecl( + hasName("::std::unique_ptr"), + decl().bind("right_class")))))) + .bind("release_member")))))) + .bind("reset_call"), + this); +} + +namespace { +const Type *getDeleterForUniquePtr(const MatchFinder::MatchResult &Result, + StringRef ID) { + const auto *Class = + Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(ID); + if (!Class) + return nullptr; + auto DeleterArgument = Class->getTemplateArgs()[1]; + if (DeleterArgument.getKind() != TemplateArgument::Type) + return nullptr; + return DeleterArgument.getAsType().getTypePtr(); +} + +bool areDeletersCompatible(const MatchFinder::MatchResult &Result) { + const Type *LeftDeleterType = getDeleterForUniquePtr(Result, "left_class"); + const Type *RightDeleterType = getDeleterForUniquePtr(Result, "right_class"); + + if (LeftDeleterType->getUnqualifiedDesugaredType() == + RightDeleterType->getUnqualifiedDesugaredType()) { + // Same type. We assume they are compatible. + // This check handles the case where the deleters are function pointers. + return true; + } + + const CXXRecordDecl *LeftDeleter = LeftDeleterType->getAsCXXRecordDecl(); + const CXXRecordDecl *RightDeleter = RightDeleterType->getAsCXXRecordDecl(); + if (!LeftDeleter || !RightDeleter) + return false; + + if (LeftDeleter->getCanonicalDecl() == RightDeleter->getCanonicalDecl()) { + // Same class. We assume they are compatible. + return true; + } + + const auto *LeftAsTemplate = + dyn_cast<ClassTemplateSpecializationDecl>(LeftDeleter); + const auto *RightAsTemplate = + dyn_cast<ClassTemplateSpecializationDecl>(RightDeleter); + if (LeftAsTemplate && RightAsTemplate && + LeftAsTemplate->getSpecializedTemplate() == + RightAsTemplate->getSpecializedTemplate()) { + // They are different instantiations of the same template. We assume they + // are compatible. + // This handles things like std::default_delete<Base> vs. + // std::default_delete<Derived>. + return true; + } + return false; +} + +} // namespace + +void UniqueptrResetReleaseCheck::check(const MatchFinder::MatchResult &Result) { + if (!areDeletersCompatible(Result)) + return; + + const auto *ResetMember = Result.Nodes.getNodeAs<MemberExpr>("reset_member"); + const auto *ReleaseMember = + Result.Nodes.getNodeAs<MemberExpr>("release_member"); + const auto *Right = Result.Nodes.getNodeAs<Expr>("right"); + const auto *ResetCall = + Result.Nodes.getNodeAs<CXXMemberCallExpr>("reset_call"); + + StringRef AssignmentText = " = "; + StringRef TrailingText = ""; + bool NeedsUtilityInclude = false; + if (ReleaseMember->isArrow()) { + AssignmentText = " = std::move(*"; + TrailingText = ")"; + NeedsUtilityInclude = true; + } else if (!Right->isPRValue()) { + AssignmentText = " = std::move("; + TrailingText = ")"; + NeedsUtilityInclude = true; + } + + auto D = diag(ResetMember->getExprLoc(), + "prefer 'unique_ptr<>' assignment over 'release' and 'reset'"); + if (ResetMember->isArrow()) + D << FixItHint::CreateInsertion(ResetMember->getBeginLoc(), "*"); + D << FixItHint::CreateReplacement( + CharSourceRange::getCharRange(ResetMember->getOperatorLoc(), + Right->getBeginLoc()), + AssignmentText) + << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(ReleaseMember->getOperatorLoc(), + ResetCall->getEndLoc()), + TrailingText); + if (NeedsUtilityInclude) + D << Inserter.createIncludeInsertion( + Result.SourceManager->getFileID(ResetMember->getBeginLoc()), + "<utility>"); +} +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h new file mode 100644 index 0000000000..a76cc77112 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h @@ -0,0 +1,50 @@ +//===--- UniqueptrResetReleaseCheck.h - clang-tidy --------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H + +#include "../ClangTidyCheck.h" +#include "../utils/IncludeInserter.h" + +namespace clang::tidy::misc { + +/// Find and replace `unique_ptr::reset(release())` with `std::move()`. +/// +/// Example: +/// +/// \code +/// std::unique_ptr<Foo> x, y; +/// x.reset(y.release()); -> x = std::move(y); +/// \endcode +/// +/// If `y` is already rvalue, `std::move()` is not added. `x` and `y` can also +/// be `std::unique_ptr<Foo>*`. +class UniqueptrResetReleaseCheck : public ClangTidyCheck { +public: + UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context); + + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + // Only register the matchers for C++11; the functionality currently does + // not + // provide any benefit to other languages, despite being benign. + return LangOpts.CPlusPlus11; + } + void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, + Preprocessor *ModuleExpanderPP) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + utils::IncludeInserter Inserter; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp new file mode 100644 index 0000000000..2dfaca19a8 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.cpp @@ -0,0 +1,54 @@ +//===--- UnusedAliasDeclsCheck.cpp - clang-tidy----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UnusedAliasDeclsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +void UnusedAliasDeclsCheck::registerMatchers(MatchFinder *Finder) { + // We cannot do anything about headers (yet), as the alias declarations + // used in one header could be used by some other translation unit. + Finder->addMatcher(namespaceAliasDecl(isExpansionInMainFile()).bind("alias"), + this); + Finder->addMatcher(nestedNameSpecifier().bind("nns"), this); +} + +void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *AliasDecl = Result.Nodes.getNodeAs<NamedDecl>("alias")) { + FoundDecls[AliasDecl] = CharSourceRange::getCharRange( + AliasDecl->getBeginLoc(), + Lexer::findLocationAfterToken( + AliasDecl->getEndLoc(), tok::semi, *Result.SourceManager, + getLangOpts(), + /*SkipTrailingWhitespaceAndNewLine=*/true)); + return; + } + + if (const auto *NestedName = + Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) { + if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) { + FoundDecls[AliasDecl] = CharSourceRange(); + } + } +} + +void UnusedAliasDeclsCheck::onEndOfTranslationUnit() { + for (const auto &FoundDecl : FoundDecls) { + if (!FoundDecl.second.isValid()) + continue; + diag(FoundDecl.first->getLocation(), "namespace alias decl %0 is unused") + << FoundDecl.first << FixItHint::CreateRemoval(FoundDecl.second); + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.h new file mode 100644 index 0000000000..9f995d94c1 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedAliasDeclsCheck.h @@ -0,0 +1,35 @@ +//===--- UnusedAliasDeclsCheck.h - clang-tidy--------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H + +#include "../ClangTidyCheck.h" +#include "llvm/ADT/DenseMap.h" + +namespace clang::tidy::misc { + +/// Finds unused namespace alias declarations. +class UnusedAliasDeclsCheck : public ClangTidyCheck { +public: + UnusedAliasDeclsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus11; + } + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; + +private: + llvm::DenseMap<const NamedDecl *, CharSourceRange> FoundDecls; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_ALIAS_DECLS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.cpp new file mode 100644 index 0000000000..2c69cb0df7 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.cpp @@ -0,0 +1,199 @@ +//===--- UnusedParametersCheck.cpp - clang-tidy----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UnusedParametersCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/ASTLambda.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/STLExtras.h" +#include <unordered_map> +#include <unordered_set> + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { +bool isOverrideMethod(const FunctionDecl *Function) { + if (const auto *MD = dyn_cast<CXXMethodDecl>(Function)) + return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>(); + return false; +} +} // namespace + +void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()), + hasAnyParameter(decl()), + unless(hasAttr(attr::Kind::Naked))) + .bind("function"), + this); +} + +template <typename T> +static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, + const T *PrevNode, const T *Node, + const T *NextNode) { + if (NextNode) + return CharSourceRange::getCharRange(Node->getBeginLoc(), + NextNode->getBeginLoc()); + + if (PrevNode) + return CharSourceRange::getTokenRange( + Lexer::getLocForEndOfToken(PrevNode->getEndLoc(), 0, + *Result.SourceManager, + Result.Context->getLangOpts()), + Node->getEndLoc()); + + return CharSourceRange::getTokenRange(Node->getSourceRange()); +} + +static FixItHint removeParameter(const MatchFinder::MatchResult &Result, + const FunctionDecl *Function, unsigned Index) { + return FixItHint::CreateRemoval(removeNode( + Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr, + Function->getParamDecl(Index), + Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1) + : nullptr)); +} + +static FixItHint removeArgument(const MatchFinder::MatchResult &Result, + const CallExpr *Call, unsigned Index) { + return FixItHint::CreateRemoval(removeNode( + Result, Index > 0 ? Call->getArg(Index - 1) : nullptr, + Call->getArg(Index), + Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr)); +} + +class UnusedParametersCheck::IndexerVisitor + : public RecursiveASTVisitor<IndexerVisitor> { +public: + IndexerVisitor(ASTContext &Ctx) { TraverseAST(Ctx); } + + const std::unordered_set<const CallExpr *> & + getFnCalls(const FunctionDecl *Fn) { + return Index[Fn->getCanonicalDecl()].Calls; + } + + const std::unordered_set<const DeclRefExpr *> & + getOtherRefs(const FunctionDecl *Fn) { + return Index[Fn->getCanonicalDecl()].OtherRefs; + } + + bool shouldTraversePostOrder() const { return true; } + + bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) { + if (const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { + Fn = Fn->getCanonicalDecl(); + Index[Fn].OtherRefs.insert(DeclRef); + } + return true; + } + + bool WalkUpFromCallExpr(CallExpr *Call) { + if (const auto *Fn = + dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) { + Fn = Fn->getCanonicalDecl(); + if (const auto *Ref = + dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) { + Index[Fn].OtherRefs.erase(Ref); + } + Index[Fn].Calls.insert(Call); + } + return true; + } + +private: + struct IndexEntry { + std::unordered_set<const CallExpr *> Calls; + std::unordered_set<const DeclRefExpr *> OtherRefs; + }; + + std::unordered_map<const FunctionDecl *, IndexEntry> Index; +}; + +UnusedParametersCheck::~UnusedParametersCheck() = default; + +UnusedParametersCheck::UnusedParametersCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + StrictMode(Options.getLocalOrGlobal("StrictMode", false)) {} + +void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "StrictMode", StrictMode); +} + +void UnusedParametersCheck::warnOnUnusedParameter( + const MatchFinder::MatchResult &Result, const FunctionDecl *Function, + unsigned ParamIndex) { + const auto *Param = Function->getParamDecl(ParamIndex); + // Don't bother to diagnose invalid parameters as being unused. + if (Param->isInvalidDecl()) + return; + auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param; + + if (!Indexer) { + Indexer = std::make_unique<IndexerVisitor>(*Result.Context); + } + + // Cannot remove parameter for non-local functions. + if (Function->isExternallyVisible() || + !Result.SourceManager->isInMainFile(Function->getLocation()) || + !Indexer->getOtherRefs(Function).empty() || isOverrideMethod(Function) || + isLambdaCallOperator(Function)) { + + // It is illegal to omit parameter name here in C code, so early-out. + if (!Result.Context->getLangOpts().CPlusPlus) + return; + + SourceRange RemovalRange(Param->getLocation()); + // Note: We always add a space before the '/*' to not accidentally create + // a '*/*' for pointer types, which doesn't start a comment. clang-format + // will clean this up afterwards. + MyDiag << FixItHint::CreateReplacement( + RemovalRange, (Twine(" /*") + Param->getName() + "*/").str()); + return; + } + + // Fix all redeclarations. + for (const FunctionDecl *FD : Function->redecls()) + if (FD->param_size()) + MyDiag << removeParameter(Result, FD, ParamIndex); + + // Fix all call sites. + for (const CallExpr *Call : Indexer->getFnCalls(Function)) + if (ParamIndex < Call->getNumArgs()) // See PR38055 for example. + MyDiag << removeArgument(Result, Call, ParamIndex); +} + +void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function"); + if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation()) + return; + if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) + if (Method->isLambdaStaticInvoker()) + return; + for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) { + const auto *Param = Function->getParamDecl(I); + if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() || + Param->hasAttr<UnusedAttr>()) + continue; + + // In non-strict mode ignore function definitions with empty bodies + // (constructor initializer counts for non-empty body). + if (StrictMode || + (Function->getBody()->child_begin() != + Function->getBody()->child_end()) || + (isa<CXXConstructorDecl>(Function) && + cast<CXXConstructorDecl>(Function)->getNumCtorInitializers() > 0)) + warnOnUnusedParameter(Result, Function, I); + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.h new file mode 100644 index 0000000000..684c95daaa --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedParametersCheck.h @@ -0,0 +1,38 @@ +//===--- UnusedParametersCheck.h - clang-tidy--------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_PARAMETERS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_PARAMETERS_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// Finds unused parameters and fixes them, so that `-Wunused-parameter` can be +/// turned on. +class UnusedParametersCheck : public ClangTidyCheck { +public: + UnusedParametersCheck(StringRef Name, ClangTidyContext *Context); + ~UnusedParametersCheck(); + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + const bool StrictMode; + class IndexerVisitor; + std::unique_ptr<IndexerVisitor> Indexer; + + void + warnOnUnusedParameter(const ast_matchers::MatchFinder::MatchResult &Result, + const FunctionDecl *Function, unsigned ParamIndex); +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_PARAMETERS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp new file mode 100644 index 0000000000..3602aa4a24 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp @@ -0,0 +1,202 @@ +//===--- UnusedUsingDeclsCheck.cpp - clang-tidy----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UnusedUsingDeclsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl, + clang::ast_matchers::internal::Matcher<NamedDecl>, DeclMatcher) { + if (const auto *TD = Node.getTemplateName().getAsTemplateDecl()) + return DeclMatcher.matches(*TD, Finder, Builder); + return false; +} + +} // namespace + +// A function that helps to tell whether a TargetDecl in a UsingDecl will be +// checked. Only variable, function, function template, class template, class, +// enum declaration and enum constant declaration are considered. +static bool shouldCheckDecl(const Decl *TargetDecl) { + return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) || + isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) || + isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) || + isa<EnumConstantDecl>(TargetDecl); +} + +UnusedUsingDeclsCheck::UnusedUsingDeclsCheck(StringRef Name, + ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + RawStringHeaderFileExtensions(Options.getLocalOrGlobal( + "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, + utils::defaultFileExtensionDelimiters())) + this->configurationDiag("Invalid header file extension: '%0'") + << RawStringHeaderFileExtensions; +} + +void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this); + auto DeclMatcher = hasDeclaration(namedDecl().bind("used")); + Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this); + Finder->addMatcher(loc(deducedTemplateSpecializationType( + refsToTemplatedDecl(namedDecl().bind("used")))), + this); + Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))), + this); + Finder->addMatcher( + callExpr(hasDeclaration(functionDecl( + forEachTemplateArgument(templateArgument().bind("used"))))), + this); + Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument( + templateArgument().bind("used")))), + this); + Finder->addMatcher(userDefinedLiteral().bind("used"), this); + // Cases where we can identify the UsingShadowDecl directly, rather than + // just its target. + // FIXME: cover more cases in this way, as the AST supports it. + auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind("usedShadow")); + Finder->addMatcher(declRefExpr(ThroughShadowMatcher), this); + Finder->addMatcher(loc(usingType(ThroughShadowMatcher)), this); +} + +void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { + if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred()) + return; + // We don't emit warnings on unused-using-decls from headers, so bail out if + // the main file is a header. + if (const auto *MainFile = Result.SourceManager->getFileEntryForID( + Result.SourceManager->getMainFileID()); + utils::isFileExtension(MainFile->getName(), HeaderFileExtensions)) + return; + + if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) { + // Ignores using-declarations defined in macros. + if (Using->getLocation().isMacroID()) + return; + + // Ignores using-declarations defined in class definition. + if (isa<CXXRecordDecl>(Using->getDeclContext())) + return; + + // FIXME: We ignore using-decls defined in function definitions at the + // moment because of false positives caused by ADL and different function + // scopes. + if (isa<FunctionDecl>(Using->getDeclContext())) + return; + + UsingDeclContext Context(Using); + Context.UsingDeclRange = CharSourceRange::getCharRange( + Using->getBeginLoc(), + Lexer::findLocationAfterToken( + Using->getEndLoc(), tok::semi, *Result.SourceManager, getLangOpts(), + /*SkipTrailingWhitespaceAndNewLine=*/true)); + for (const auto *UsingShadow : Using->shadows()) { + const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl(); + if (shouldCheckDecl(TargetDecl)) + Context.UsingTargetDecls.insert(TargetDecl); + } + if (!Context.UsingTargetDecls.empty()) + Contexts.push_back(Context); + return; + } + + // Mark a corresponding using declaration as used. + auto RemoveNamedDecl = [&](const NamedDecl *Used) { + removeFromFoundDecls(Used); + // Also remove variants of Used. + if (const auto *FD = dyn_cast<FunctionDecl>(Used)) { + removeFromFoundDecls(FD->getPrimaryTemplate()); + } else if (const auto *Specialization = + dyn_cast<ClassTemplateSpecializationDecl>(Used)) { + removeFromFoundDecls(Specialization->getSpecializedTemplate()); + } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(Used)) { + if (const auto *ET = ECD->getType()->getAs<EnumType>()) + removeFromFoundDecls(ET->getDecl()); + } + }; + // We rely on the fact that the clang AST is walked in order, usages are only + // marked after a corresponding using decl has been found. + if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) { + RemoveNamedDecl(Used); + return; + } + + if (const auto *UsedShadow = + Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) { + removeFromFoundDecls(UsedShadow->getTargetDecl()); + return; + } + + if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) { + if (Used->getKind() == TemplateArgument::Template) { + if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl()) + removeFromFoundDecls(TD); + } else if (Used->getKind() == TemplateArgument::Type) { + if (auto *RD = Used->getAsType()->getAsCXXRecordDecl()) + removeFromFoundDecls(RD); + } else if (Used->getKind() == TemplateArgument::Declaration) { + RemoveNamedDecl(Used->getAsDecl()); + } + return; + } + + if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) { + RemoveNamedDecl(DRE->getDecl()); + return; + } + // Check the uninstantiated template function usage. + if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) { + for (const NamedDecl *ND : ULE->decls()) { + if (const auto *USD = dyn_cast<UsingShadowDecl>(ND)) + removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl()); + } + return; + } + // Check user-defined literals + if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used")) + removeFromFoundDecls(UDL->getCalleeDecl()); +} + +void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) { + if (!D) + return; + // FIXME: Currently, we don't handle the using-decls being used in different + // scopes (such as different namespaces, different functions). Instead of + // giving an incorrect message, we mark all of them as used. + // + // FIXME: Use a more efficient way to find a matching context. + for (auto &Context : Contexts) { + if (Context.UsingTargetDecls.contains(D->getCanonicalDecl())) + Context.IsUsed = true; + } +} + +void UnusedUsingDeclsCheck::onEndOfTranslationUnit() { + for (const auto &Context : Contexts) { + if (!Context.IsUsed) { + diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused") + << Context.FoundUsingDecl; + // Emit a fix and a fix description of the check; + diag(Context.FoundUsingDecl->getLocation(), + /*Description=*/"remove the using", DiagnosticIDs::Note) + << FixItHint::CreateRemoval(Context.UsingDeclRange); + } + } + Contexts.clear(); +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.h new file mode 100644 index 0000000000..5ca7aef9df --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UnusedUsingDeclsCheck.h @@ -0,0 +1,56 @@ +//===--- UnusedUsingDeclsCheck.h - clang-tidy--------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H + +#include "../ClangTidyCheck.h" +#include "../utils/FileExtensionsUtils.h" +#include "llvm/ADT/SmallPtrSet.h" +#include <vector> + +namespace clang::tidy::misc { + +/// Finds unused using declarations. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/unused-using-decls.html +class UnusedUsingDeclsCheck : public ClangTidyCheck { +public: + UnusedUsingDeclsCheck(StringRef Name, ClangTidyContext *Context); + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; + +private: + void removeFromFoundDecls(const Decl *D); + + struct UsingDeclContext { + explicit UsingDeclContext(const UsingDecl *FoundUsingDecl) + : FoundUsingDecl(FoundUsingDecl), IsUsed(false) {} + // A set saves all UsingShadowDecls introduced by a UsingDecl. A UsingDecl + // can introduce multiple UsingShadowDecls in some cases (such as + // overloaded functions). + llvm::SmallPtrSet<const Decl *, 4> UsingTargetDecls; + // The original UsingDecl. + const UsingDecl *FoundUsingDecl; + // The source range of the UsingDecl. + CharSourceRange UsingDeclRange; + // Whether the UsingDecl is used. + bool IsUsed; + }; + + std::vector<UsingDeclContext> Contexts; + + const StringRef RawStringHeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_USING_DECLS_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp new file mode 100644 index 0000000000..7e4bba216f --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp @@ -0,0 +1,75 @@ +//===--- UseAnonymousNamespaceCheck.cpp - clang-tidy ----------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "UseAnonymousNamespaceCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { +namespace { +AST_POLYMORPHIC_MATCHER_P(isInHeaderFile, + AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, + VarDecl), + utils::FileExtensionsSet, HeaderFileExtensions) { + return utils::isExpansionLocInHeaderFile( + Node.getBeginLoc(), Finder->getASTContext().getSourceManager(), + HeaderFileExtensions); +} + +AST_MATCHER(FunctionDecl, isMemberFunction) { + return llvm::isa<CXXMethodDecl>(&Node); +} +AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); } +} // namespace + +UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + RawStringHeaderFileExtensions(Options.getLocalOrGlobal( + "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, + utils::defaultFileExtensionDelimiters())) { + this->configurationDiag("Invalid header file extension: '%0'") + << RawStringHeaderFileExtensions; + } +} + +void UseAnonymousNamespaceCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); +} + +void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl(isStaticStorageClass(), + unless(anyOf(isInHeaderFile(HeaderFileExtensions), + isInAnonymousNamespace(), isMemberFunction()))) + .bind("x"), + this); + Finder->addMatcher( + varDecl(isStaticStorageClass(), + unless(anyOf(isInHeaderFile(HeaderFileExtensions), + isInAnonymousNamespace(), isStaticLocal(), + isStaticDataMember(), hasType(isConstQualified())))) + .bind("x"), + this); +} + +void UseAnonymousNamespaceCheck::check(const MatchFinder::MatchResult &Result) { + if (const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>("x")) { + StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function"; + diag(MatchedDecl->getLocation(), + "%0 %1 declared 'static', move to anonymous namespace instead") + << Type << MatchedDecl; + } +} + +} // namespace clang::tidy::misc diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h new file mode 100644 index 0000000000..b3c10a1249 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h @@ -0,0 +1,47 @@ +//===--- UseAnonymousNamespaceCheck.h - clang-tidy --------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H + +#include "../ClangTidyCheck.h" +#include "../utils/FileExtensionsUtils.h" + +namespace clang::tidy::misc { + +/// Warns when using 'static' functions or variables at global scope, and +/// suggests moving them to an anonymous namespace. +/// +/// The check supports these options: +/// - `HeaderFileExtensions`: a semicolon-separated list of filename +/// extensions of header files (The filename extension should not contain +/// "." prefix). ";h;hh;hpp;hxx" by default. +/// +/// For extension-less header files, using an empty string or leaving an +/// empty string between ";" if there are other filename extensions. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc/use-anonymous-namespace.html +class UseAnonymousNamespaceCheck : public ClangTidyCheck { +public: + UseAnonymousNamespaceCheck(StringRef Name, ClangTidyContext *Context); + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const StringRef RawStringHeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/misc/ya.make b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ya.make new file mode 100644 index 0000000000..0e5ac971f9 --- /dev/null +++ b/contrib/libs/clang16/tools/extra/clang-tidy/misc/ya.make @@ -0,0 +1,60 @@ +# Generated by devtools/yamaker. + +LIBRARY() + +LICENSE(Apache-2.0 WITH LLVM-exception) + +LICENSE_TEXTS(.yandex_meta/licenses.list.txt) + +PEERDIR( + contrib/libs/clang16 + contrib/libs/clang16/include + contrib/libs/clang16/lib + contrib/libs/clang16/lib/AST + contrib/libs/clang16/lib/ASTMatchers + contrib/libs/clang16/lib/Analysis + contrib/libs/clang16/lib/Basic + contrib/libs/clang16/lib/Lex + contrib/libs/clang16/lib/Serialization + contrib/libs/clang16/lib/Tooling + contrib/libs/clang16/tools/extra/clang-tidy/utils + contrib/libs/llvm16 + contrib/libs/llvm16/lib/Frontend/OpenMP + contrib/libs/llvm16/lib/Support +) + +ADDINCL( + ${ARCADIA_BUILD_ROOT}/contrib/libs/clang16/tools/extra/clang-tidy + contrib/libs/clang16/tools/extra/clang-tidy + ${ARCADIA_BUILD_ROOT}/contrib/libs/clang16/tools/extra/clang-tidy/misc + contrib/libs/clang16/tools/extra/clang-tidy/misc +) + +NO_COMPILER_WARNINGS() + +NO_UTIL() + +SRCS( + ConfusableIdentifierCheck.cpp + ConstCorrectnessCheck.cpp + DefinitionsInHeadersCheck.cpp + MiscTidyModule.cpp + MisleadingBidirectional.cpp + MisleadingIdentifier.cpp + MisplacedConstCheck.cpp + NewDeleteOverloadsCheck.cpp + NoRecursionCheck.cpp + NonCopyableObjects.cpp + NonPrivateMemberVariablesInClassesCheck.cpp + RedundantExpressionCheck.cpp + StaticAssertCheck.cpp + ThrowByValueCatchByReferenceCheck.cpp + UnconventionalAssignOperatorCheck.cpp + UniqueptrResetReleaseCheck.cpp + UnusedAliasDeclsCheck.cpp + UnusedParametersCheck.cpp + UnusedUsingDeclsCheck.cpp + UseAnonymousNamespaceCheck.cpp +) + +END() |