//===--- IsolateDeclarationCheck.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 "IsolateDeclarationCheck.h" #include "../utils/LexerUtils.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include <optional> usingnamespaceclang::ast_matchers; usingnamespaceclang::tidy::utils::lexer; namespace clang::tidy::readability { namespace { AST_MATCHER(DeclStmt, isSingleDecl) { … } AST_MATCHER(DeclStmt, onlyDeclaresVariables) { … } } // namespace void IsolateDeclarationCheck::registerMatchers(MatchFinder *Finder) { … } static SourceLocation findStartOfIndirection(SourceLocation Start, int Indirections, const SourceManager &SM, const LangOptions &LangOpts) { … } static bool isMacroID(SourceRange R) { … } /// This function counts the number of written indirections for the given /// Type \p T. It does \b NOT resolve typedefs as it's a helper for lexing /// the source code. /// \see declRanges static int countIndirections(const Type *T, int Indirections = 0) { … } static bool typeIsMemberPointer(const Type *T) { … } /// This function tries to extract the SourceRanges that make up all /// declarations in this \c DeclStmt. /// /// The resulting vector has the structure {UnderlyingType, Decl1, Decl2, ...}. /// Each \c SourceRange is of the form [Begin, End). /// If any of the create ranges is invalid or in a macro the result will be /// \c None. /// If the \c DeclStmt contains only one declaration, the result is \c None. /// If the \c DeclStmt contains declarations other than \c VarDecl the result /// is \c None. /// /// \code /// int * ptr1 = nullptr, value = 42; /// // [ ][ ] [ ] - The ranges here are inclusive /// \endcode /// \todo Generalize this function to take other declarations than \c VarDecl. static std::optional<std::vector<SourceRange>> declRanges(const DeclStmt *DS, const SourceManager &SM, const LangOptions &LangOpts) { … } static std::optional<std::vector<StringRef>> collectSourceRanges(llvm::ArrayRef<SourceRange> Ranges, const SourceManager &SM, const LangOptions &LangOpts) { … } /// Expects a vector {TypeSnippet, Firstdecl, SecondDecl, ...}. static std::vector<std::string> createIsolatedDecls(llvm::ArrayRef<StringRef> Snippets) { … } void IsolateDeclarationCheck::check(const MatchFinder::MatchResult &Result) { … } } // namespace clang::tidy::readability