Line | Count | Source |
1 | //===- UnsafeBufferUsage.h - Replace pointers with modern C++ ---*- 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 | // This file defines an analysis that aids replacing buffer accesses through | |
10 | // raw pointers with safer C++ abstractions such as containers and views/spans. | |
11 | // | |
12 | //===----------------------------------------------------------------------===// | |
13 | ||
14 | #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H | |
15 | #define LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H | |
16 | ||
17 | #include "clang/AST/Decl.h" | |
18 | #include "clang/AST/Stmt.h" | |
19 | ||
20 | namespace clang { | |
21 | ||
22 | using DefMapTy = llvm::DenseMap<const VarDecl *, std::vector<const VarDecl *>>; | |
23 | ||
24 | /// The interface that lets the caller handle unsafe buffer usage analysis | |
25 | /// results by overriding this class's handle... methods. | |
26 | class UnsafeBufferUsageHandler { | |
27 | public: | |
28 | 71.9k | UnsafeBufferUsageHandler() = default; |
29 | 71.9k | virtual ~UnsafeBufferUsageHandler() = default; |
30 | ||
31 | /// This analyses produces large fixits that are organized into lists | |
32 | /// of primitive fixits (individual insertions/removals/replacements). | |
33 | using FixItList = llvm::SmallVectorImpl<FixItHint>; | |
34 | ||
35 | /// Invoked when an unsafe operation over raw pointers is found. | |
36 | virtual void handleUnsafeOperation(const Stmt *Operation, | |
37 | bool IsRelatedToDecl) = 0; | |
38 | ||
39 | /// Invoked when a fix is suggested against a variable. This function groups | |
40 | /// all variables that must be fixed together (i.e their types must be changed to the | |
41 | /// same target type to prevent type mismatches) into a single fixit. | |
42 | virtual void handleUnsafeVariableGroup(const VarDecl *Variable, | |
43 | const DefMapTy &VarGrpMap, | |
44 | FixItList &&Fixes) = 0; | |
45 | ||
46 | /// Returns a reference to the `Preprocessor`: | |
47 | virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const = 0; | |
48 | ||
49 | /// Returns the text indicating that the user needs to provide input there: | |
50 | virtual std::string | |
51 | 191 | getUserFillPlaceHolder(StringRef HintTextToUser = "placeholder") const { |
52 | 191 | std::string s = std::string("<# "); |
53 | 191 | s += HintTextToUser; |
54 | 191 | s += " #>"; |
55 | 191 | return s; |
56 | 191 | } |
57 | }; | |
58 | ||
59 | // This function invokes the analysis and allows the caller to react to it | |
60 | // through the handler class. | |
61 | void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler, | |
62 | bool EmitSuggestions); | |
63 | ||
64 | namespace internal { | |
65 | // Tests if any two `FixItHint`s in `FixIts` conflict. Two `FixItHint`s | |
66 | // conflict if they have overlapping source ranges. | |
67 | bool anyConflict(const llvm::SmallVectorImpl<FixItHint> &FixIts, | |
68 | const SourceManager &SM); | |
69 | } // namespace internal | |
70 | } // end namespace clang | |
71 | ||
72 | #endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */ |