diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/cppcoreguidelines')
4 files changed, 0 insertions, 175 deletions
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt index d466ca83566..3bf729cbd10 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CMakeLists.txt @@ -3,7 +3,6 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyCppCoreGuidelinesModule CppCoreGuidelinesTidyModule.cpp ProBoundsArrayToPointerDecayCheck.cpp - ProBoundsConstantArrayIndexCheck.cpp ProBoundsPointerArithmeticCheck.cpp ProTypeConstCastCheck.cpp ProTypeCstyleCastCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp index db783a3975d..1ef55da14ab 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -12,7 +12,6 @@ #include "../ClangTidyModuleRegistry.h" #include "../misc/AssignOperatorSignatureCheck.h" #include "ProBoundsArrayToPointerDecayCheck.h" -#include "ProBoundsConstantArrayIndexCheck.h" #include "ProBoundsPointerArithmeticCheck.h" #include "ProTypeConstCastCheck.h" #include "ProTypeCstyleCastCheck.h" @@ -31,8 +30,6 @@ public: void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<ProBoundsArrayToPointerDecayCheck>( "cppcoreguidelines-pro-bounds-array-to-pointer-decay"); - CheckFactories.registerCheck<ProBoundsConstantArrayIndexCheck>( - "cppcoreguidelines-pro-bounds-constant-array-index"); CheckFactories.registerCheck<ProBoundsPointerArithmeticCheck>( "cppcoreguidelines-pro-bounds-pointer-arithmetic"); CheckFactories.registerCheck<ProTypeConstCastCheck>( diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp deleted file mode 100644 index b976cb24b15..00000000000 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ /dev/null @@ -1,131 +0,0 @@ -//===--- ProBoundsConstantArrayIndexCheck.cpp - clang-tidy-----------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "ProBoundsConstantArrayIndexCheck.h" -#include "clang/AST/ASTContext.h" -#include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Lex/Preprocessor.h" - -using namespace clang::ast_matchers; - -namespace clang { -namespace tidy { - -ProBoundsConstantArrayIndexCheck::ProBoundsConstantArrayIndexCheck( - StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context), GslHeader(Options.get("GslHeader", "")), - IncludeStyle(IncludeSorter::parseIncludeStyle( - Options.get("IncludeStyle", "llvm"))) {} - -void ProBoundsConstantArrayIndexCheck::storeOptions( - ClangTidyOptions::OptionMap &Opts) { - Options.store(Opts, "GslHeader", GslHeader); -} - -void ProBoundsConstantArrayIndexCheck::registerPPCallbacks( - CompilerInstance &Compiler) { - if (!getLangOpts().CPlusPlus) - return; - - Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), - Compiler.getLangOpts(), IncludeStyle)); - Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); -} - -void ProBoundsConstantArrayIndexCheck::registerMatchers(MatchFinder *Finder) { - if (!getLangOpts().CPlusPlus) - return; - - Finder->addMatcher(arraySubscriptExpr(hasBase(ignoringImpCasts(hasType( - constantArrayType().bind("type")))), - hasIndex(expr().bind("index"))) - .bind("expr"), - this); - - Finder->addMatcher( - cxxOperatorCallExpr( - hasOverloadedOperatorName("[]"), - hasArgument( - 0, hasType(cxxRecordDecl(hasName("::std::array")).bind("type"))), - hasArgument(1, expr().bind("index"))) - .bind("expr"), - this); -} - -void ProBoundsConstantArrayIndexCheck::check( - const MatchFinder::MatchResult &Result) { - const auto *Matched = Result.Nodes.getNodeAs<Expr>("expr"); - const auto *IndexExpr = Result.Nodes.getNodeAs<Expr>("index"); - llvm::APSInt Index; - if (!IndexExpr->isIntegerConstantExpr(Index, *Result.Context, nullptr, - /*isEvaluated=*/true)) { - SourceRange BaseRange; - if (const auto *ArraySubscriptE = dyn_cast<ArraySubscriptExpr>(Matched)) - BaseRange = ArraySubscriptE->getBase()->getSourceRange(); - else - BaseRange = - dyn_cast<CXXOperatorCallExpr>(Matched)->getArg(0)->getSourceRange(); - SourceRange IndexRange = IndexExpr->getSourceRange(); - - auto Diag = diag(Matched->getExprLoc(), - "do not use array subscript when the index is " - "not a compile-time constant; use gsl::at() " - "instead"); - if (!GslHeader.empty()) { - Diag << FixItHint::CreateInsertion(BaseRange.getBegin(), "gsl::at(") - << FixItHint::CreateReplacement( - SourceRange(BaseRange.getEnd().getLocWithOffset(1), - IndexRange.getBegin().getLocWithOffset(-1)), - ", ") - << FixItHint::CreateReplacement(Matched->getLocEnd(), ")"); - - auto Insertion = Inserter->CreateIncludeInsertion( - Result.SourceManager->getMainFileID(), GslHeader, - /*IsAngled=*/false); - if (Insertion.hasValue()) - Diag << Insertion.getValue(); - } - return; - } - - const auto *StdArrayDecl = - Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("type"); - - // For static arrays, this is handled in clang-diagnostic-array-bounds. - if (!StdArrayDecl) - return; - - if (Index.isSigned() && Index.isNegative()) { - diag(Matched->getExprLoc(), - "std::array<> index %0 is before the beginning of the array") - << Index.toString(10); - return; - } - - const auto &TemplateArgs = StdArrayDecl->getTemplateArgs(); - if (TemplateArgs.size() < 2) - return; - // First template arg of std::array is the type, second arg is the size. - const auto &SizeArg = TemplateArgs[1]; - if (SizeArg.getKind() != TemplateArgument::Integral) - return; - llvm::APInt ArraySize = SizeArg.getAsIntegral(); - - // Get uint64_t values, because different bitwidths would lead to an assertion - // in APInt::uge. - if (Index.getZExtValue() >= ArraySize.getZExtValue()) { - diag(Matched->getExprLoc(), "std::array<> index %0 is past the end of the array " - "(which contains %1 elements)") - << Index.toString(10) << ArraySize.toString(10, false); - } -} - -} // namespace tidy -} // namespace clang diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h deleted file mode 100644 index d95286b906f..00000000000 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h +++ /dev/null @@ -1,40 +0,0 @@ -//===--- ProBoundsConstantArrayIndexCheck.h - clang-tidy---------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_CONSTANT_ARRAY_INDEX_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_CONSTANT_ARRAY_INDEX_H - -#include "../ClangTidy.h" -#include "../utils/IncludeInserter.h" - -namespace clang { -namespace tidy { - -/// This checks that all array subscriptions on static arrays and std::arrays -/// have a constant index and are within bounds -/// -/// For the user-facing documentation see: -/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.html -class ProBoundsConstantArrayIndexCheck : public ClangTidyCheck { - std::string GslHeader; - const IncludeSorter::IncludeStyle IncludeStyle; - std::unique_ptr<IncludeInserter> Inserter; - -public: - ProBoundsConstantArrayIndexCheck(StringRef Name, ClangTidyContext *Context); - void registerPPCallbacks(CompilerInstance &Compiler) override; - void registerMatchers(ast_matchers::MatchFinder *Finder) override; - void storeOptions(ClangTidyOptions::OptionMap &Opts) override; - void check(const ast_matchers::MatchFinder::MatchResult &Result) override; -}; - -} // namespace tidy -} // namespace clang - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_PRO_BOUNDS_CONSTANT_ARRAY_INDEX_H |