1 //===--- IdDependentBackwardBranchCheck.h - clang-tidy ----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace altera {
17 
18 /// Finds ID-dependent variables and fields used within loops, and warns of
19 /// their usage. Using these variables in loops can lead to performance
20 /// degradation.
21 ///
22 /// For the user-facing documentation see:
23 /// http://clang.llvm.org/extra/clang-tidy/checks/altera/id-dependent-backward-branch.html
24 class IdDependentBackwardBranchCheck : public ClangTidyCheck {
25 private:
26   enum LoopType { UnknownLoop = -1, DoLoop = 0, WhileLoop = 1, ForLoop = 2 };
27   // Stores information necessary for printing out source of error.
28   struct IdDependencyRecord {
IdDependencyRecordIdDependencyRecord29     IdDependencyRecord(const VarDecl *Declaration, SourceLocation Location,
30                        const llvm::Twine &Message)
31         : VariableDeclaration(Declaration), Location(Location),
32           Message(Message.str()) {}
IdDependencyRecordIdDependencyRecord33     IdDependencyRecord(const FieldDecl *Declaration, SourceLocation Location,
34                        const llvm::Twine &Message)
35         : FieldDeclaration(Declaration), Location(Location),
36           Message(Message.str()) {}
37     IdDependencyRecord() = default;
38     const VarDecl *VariableDeclaration = nullptr;
39     const FieldDecl *FieldDeclaration = nullptr;
40     SourceLocation Location;
41     std::string Message;
42   };
43   // Stores the locations where ID-dependent variables are created.
44   std::map<const VarDecl *, IdDependencyRecord> IdDepVarsMap;
45   // Stores the locations where ID-dependent fields are created.
46   std::map<const FieldDecl *, IdDependencyRecord> IdDepFieldsMap;
47   /// Returns an IdDependencyRecord if the Expression contains an ID-dependent
48   /// variable, returns a nullptr otherwise.
49   IdDependencyRecord *hasIdDepVar(const Expr *Expression);
50   /// Returns an IdDependencyRecord if the Expression contains an ID-dependent
51   /// field, returns a nullptr otherwise.
52   IdDependencyRecord *hasIdDepField(const Expr *Expression);
53   /// Stores the location an ID-dependent variable is created from a call to
54   /// an ID function in IdDepVarsMap.
55   void saveIdDepVar(const Stmt *Statement, const VarDecl *Variable);
56   /// Stores the location an ID-dependent field is created from a call to an ID
57   /// function in IdDepFieldsMap.
58   void saveIdDepField(const Stmt *Statement, const FieldDecl *Field);
59   /// Stores the location an ID-dependent variable is created from a reference
60   /// to another ID-dependent variable or field in IdDepVarsMap.
61   void saveIdDepVarFromReference(const DeclRefExpr *RefExpr,
62                                  const MemberExpr *MemExpr,
63                                  const VarDecl *PotentialVar);
64   /// Stores the location an ID-dependent field is created from a reference to
65   /// another ID-dependent variable or field in IdDepFieldsMap.
66   void saveIdDepFieldFromReference(const DeclRefExpr *RefExpr,
67                                    const MemberExpr *MemExpr,
68                                    const FieldDecl *PotentialField);
69   /// Returns the loop type.
70   LoopType getLoopType(const Stmt *Loop);
71 
72 public:
IdDependentBackwardBranchCheck(StringRef Name,ClangTidyContext * Context)73   IdDependentBackwardBranchCheck(StringRef Name, ClangTidyContext *Context)
74       : ClangTidyCheck(Name, Context) {}
75   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
76   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
77 };
78 
79 } // namespace altera
80 } // namespace tidy
81 } // namespace clang
82 
83 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_IDDEPENDENTBACKWARDBRANCHCHECK_H
84