Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:18

0001 //===--- IdDependentBackwardBranchCheck.h - clang-tidy ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::altera {
0015 
0016 /// Finds ID-dependent variables and fields used within loops, and warns of
0017 /// their usage. Using these variables in loops can lead to performance
0018 /// degradation.
0019 ///
0020 /// For the user-facing documentation see:
0021 /// http://clang.llvm.org/extra/clang-tidy/checks/altera/id-dependent-backward-branch.html
0022 class IdDependentBackwardBranchCheck : public ClangTidyCheck {
0023 private:
0024   enum LoopType { UnknownLoop = -1, DoLoop = 0, WhileLoop = 1, ForLoop = 2 };
0025   // Stores information necessary for printing out source of error.
0026   struct IdDependencyRecord {
0027     IdDependencyRecord(const VarDecl *Declaration, SourceLocation Location,
0028                        const llvm::Twine &Message)
0029         : VariableDeclaration(Declaration), Location(Location),
0030           Message(Message.str()) {}
0031     IdDependencyRecord(const FieldDecl *Declaration, SourceLocation Location,
0032                        const llvm::Twine &Message)
0033         : FieldDeclaration(Declaration), Location(Location),
0034           Message(Message.str()) {}
0035     IdDependencyRecord() = default;
0036     const VarDecl *VariableDeclaration = nullptr;
0037     const FieldDecl *FieldDeclaration = nullptr;
0038     SourceLocation Location;
0039     std::string Message;
0040   };
0041   // Stores the locations where ID-dependent variables are created.
0042   std::map<const VarDecl *, IdDependencyRecord> IdDepVarsMap;
0043   // Stores the locations where ID-dependent fields are created.
0044   std::map<const FieldDecl *, IdDependencyRecord> IdDepFieldsMap;
0045   /// Returns an IdDependencyRecord if the Expression contains an ID-dependent
0046   /// variable, returns a nullptr otherwise.
0047   IdDependencyRecord *hasIdDepVar(const Expr *Expression);
0048   /// Returns an IdDependencyRecord if the Expression contains an ID-dependent
0049   /// field, returns a nullptr otherwise.
0050   IdDependencyRecord *hasIdDepField(const Expr *Expression);
0051   /// Stores the location an ID-dependent variable is created from a call to
0052   /// an ID function in IdDepVarsMap.
0053   void saveIdDepVar(const Stmt *Statement, const VarDecl *Variable);
0054   /// Stores the location an ID-dependent field is created from a call to an ID
0055   /// function in IdDepFieldsMap.
0056   void saveIdDepField(const Stmt *Statement, const FieldDecl *Field);
0057   /// Stores the location an ID-dependent variable is created from a reference
0058   /// to another ID-dependent variable or field in IdDepVarsMap.
0059   void saveIdDepVarFromReference(const DeclRefExpr *RefExpr,
0060                                  const MemberExpr *MemExpr,
0061                                  const VarDecl *PotentialVar);
0062   /// Stores the location an ID-dependent field is created from a reference to
0063   /// another ID-dependent variable or field in IdDepFieldsMap.
0064   void saveIdDepFieldFromReference(const DeclRefExpr *RefExpr,
0065                                    const MemberExpr *MemExpr,
0066                                    const FieldDecl *PotentialField);
0067   /// Returns the loop type.
0068   LoopType getLoopType(const Stmt *Loop);
0069 
0070 public:
0071   IdDependentBackwardBranchCheck(StringRef Name, ClangTidyContext *Context)
0072       : ClangTidyCheck(Name, Context) {}
0073   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0074   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0075 };
0076 
0077 } // namespace clang::tidy::altera
0078 
0079 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H