|
|
|||
File indexing completed on 2026-05-10 08:36:22
0001 //===------------- ExprSequence.h - clang-tidy ----------------------------===// 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_EXPRSEQUENCE_H 0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_EXPRSEQUENCE_H 0011 0012 #include "clang/Analysis/CFG.h" 0013 #include "clang/Lex/Lexer.h" 0014 #include "llvm/ADT/DenseMap.h" 0015 #include "llvm/ADT/SmallPtrSet.h" 0016 #include "llvm/ADT/SmallVector.h" 0017 0018 #include "../ClangTidy.h" 0019 0020 namespace clang::tidy::utils { 0021 0022 /// Provides information about the evaluation order of (sub-)expressions within 0023 /// a `CFGBlock`. 0024 /// 0025 /// While a `CFGBlock` does contain individual `CFGElement`s for some 0026 /// sub-expressions, the order in which those `CFGElement`s appear reflects 0027 /// only one possible order in which the sub-expressions may be evaluated. 0028 /// However, we want to warn if any of the potential evaluation orders can lead 0029 /// to a use-after-move, not just the one contained in the `CFGBlock`. 0030 /// 0031 /// This class implements only a simplified version of the C++ sequencing 0032 /// rules. The main limitation is that we do not distinguish between value 0033 /// computation and side effect -- see the "Implementation" section for more 0034 /// details. 0035 /// 0036 /// Note: `SequenceChecker` from SemaChecking.cpp does a similar job (and much 0037 /// more thoroughly), but using it would require 0038 /// - Pulling `SequenceChecker` out into a header file (i.e. making it part of 0039 /// the API), 0040 /// - Removing the dependency of `SequenceChecker` on `Sema`, and 0041 /// - (Probably) modifying `SequenceChecker` to make it suitable to be used in 0042 /// this context. 0043 /// For the moment, it seems preferable to re-implement our own version of 0044 /// sequence checking that is special-cased to what we need here. 0045 /// 0046 /// Implementation 0047 /// -------------- 0048 /// 0049 /// `ExprSequence` uses two types of sequencing edges between nodes in the AST: 0050 /// 0051 /// - Every `Stmt` is assumed to be sequenced after its children. This is 0052 /// overly optimistic because the standard only states that value computations 0053 /// of operands are sequenced before the value computation of the operator, 0054 /// making no guarantees about side effects (in general). 0055 /// 0056 /// For our purposes, this rule is sufficient, however, because this check is 0057 /// interested in operations on objects, which are generally performed through 0058 /// function calls (whether explicit and implicit). Function calls guarantee 0059 /// that the value computations and side effects for all function arguments 0060 /// are sequenced before the execution of the function. 0061 /// 0062 /// - In addition, some `Stmt`s are known to be sequenced before or after 0063 /// their siblings. For example, the `Stmt`s that make up a `CompoundStmt`are 0064 /// all sequenced relative to each other. The function 0065 /// `getSequenceSuccessor()` implements these sequencing rules. 0066 class ExprSequence { 0067 public: 0068 /// Initializes this `ExprSequence` with sequence information for the given 0069 /// `CFG`. `Root` is the root statement the CFG was built from. 0070 ExprSequence(const CFG *TheCFG, const Stmt *Root, ASTContext *TheContext); 0071 0072 /// Returns whether \p Before is sequenced before \p After. 0073 bool inSequence(const Stmt *Before, const Stmt *After) const; 0074 0075 /// Returns whether \p After can potentially be evaluated after \p Before. 0076 /// This is exactly equivalent to `!inSequence(After, Before)` but makes some 0077 /// conditions read more naturally. 0078 bool potentiallyAfter(const Stmt *After, const Stmt *Before) const; 0079 0080 private: 0081 // Returns the sibling of \p S (if any) that is directly sequenced after \p S, 0082 // or nullptr if no such sibling exists. For example, if \p S is the child of 0083 // a `CompoundStmt`, this would return the Stmt that directly follows \p S in 0084 // the `CompoundStmt`. 0085 // 0086 // As the sequencing of many constructs that change control flow is already 0087 // encoded in the `CFG`, this function only implements the sequencing rules 0088 // for those constructs where sequencing cannot be inferred from the `CFG`. 0089 const Stmt *getSequenceSuccessor(const Stmt *S) const; 0090 0091 const Stmt *resolveSyntheticStmt(const Stmt *S) const; 0092 0093 ASTContext *Context; 0094 const Stmt *Root; 0095 0096 llvm::DenseMap<const Stmt *, const Stmt *> SyntheticStmtSourceMap; 0097 }; 0098 0099 /// Maps `Stmt`s to the `CFGBlock` that contains them. Some `Stmt`s may be 0100 /// contained in more than one `CFGBlock`; in this case, they are mapped to the 0101 /// innermost block (i.e. the one that is furthest from the root of the tree). 0102 class StmtToBlockMap { 0103 public: 0104 /// Initializes the map for the given `CFG`. 0105 StmtToBlockMap(const CFG *TheCFG, ASTContext *TheContext); 0106 0107 /// Returns the block that \p S is contained in. Some `Stmt`s may be contained 0108 /// in more than one `CFGBlock`; in this case, this function returns the 0109 /// innermost block (i.e. the one that is furthest from the root of the tree). 0110 const CFGBlock *blockContainingStmt(const Stmt *S) const; 0111 0112 private: 0113 ASTContext *Context; 0114 0115 llvm::DenseMap<const Stmt *, const CFGBlock *> Map; 0116 }; 0117 0118 } // namespace clang::tidy::utils 0119 0120 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_EXPRSEQUENCE_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|