clang 18.0.0git
UnsafeBufferUsage.h
Go to the documentation of this file.
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#include "llvm/Support/Debug.h"
20
21namespace clang {
22
23using VarGrpTy = std::vector<const VarDecl *>;
25
27public:
29 virtual ~VariableGroupsManager() = default;
30 /// Returns the set of variables (including `Var`) that need to be fixed
31 /// together in one step.
32 ///
33 /// `Var` must be a variable that needs fix (so it must be in a group).
34 /// `HasParm` is an optional argument that will be set to true if the set of
35 /// variables, where `Var` is in, contains parameters.
36 virtual VarGrpRef getGroupOfVar(const VarDecl *Var,
37 bool *HasParm = nullptr) const =0;
38
39 /// Returns the non-empty group of variables that include parameters of the
40 /// analyzing function, if such a group exists. An empty group, otherwise.
41 virtual VarGrpRef getGroupOfParms() const =0;
42};
43
44/// The interface that lets the caller handle unsafe buffer usage analysis
45/// results by overriding this class's handle... methods.
47#ifndef NDEBUG
48public:
49 // A self-debugging facility that you can use to notify the user when
50 // suggestions or fixits are incomplete.
51 // Uses std::function to avoid computing the message when it won't
52 // actually be displayed.
53 using DebugNote = std::pair<SourceLocation, std::string>;
54 using DebugNoteList = std::vector<DebugNote>;
55 using DebugNoteByVar = std::map<const VarDecl *, DebugNoteList>;
57#endif
58
59public:
61 virtual ~UnsafeBufferUsageHandler() = default;
62
63 /// This analyses produces large fixits that are organized into lists
64 /// of primitive fixits (individual insertions/removals/replacements).
66
67 /// Invoked when an unsafe operation over raw pointers is found.
68 virtual void handleUnsafeOperation(const Stmt *Operation,
69 bool IsRelatedToDecl) = 0;
70
71 /// Invoked when a fix is suggested against a variable. This function groups
72 /// all variables that must be fixed together (i.e their types must be changed
73 /// to the same target type to prevent type mismatches) into a single fixit.
74 ///
75 /// `D` is the declaration of the callable under analysis that owns `Variable`
76 /// and all of its group mates.
77 virtual void handleUnsafeVariableGroup(const VarDecl *Variable,
78 const VariableGroupsManager &VarGrpMgr,
79 FixItList &&Fixes, const Decl *D) = 0;
80
81#ifndef NDEBUG
82public:
84 DEBUG_WITH_TYPE("SafeBuffers", return true);
85 return false;
86 }
87
89 std::string Text) {
91 DebugNotesByVar[VD].push_back(std::make_pair(Loc, Text));
92 }
93
96 DebugNotesByVar.clear();
97 }
98#endif
99
100public:
101 /// Returns a reference to the `Preprocessor`:
102 virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const = 0;
103
104 virtual std::string
106 StringRef WSSuffix = "") const = 0;
107};
108
109// This function invokes the analysis and allows the caller to react to it
110// through the handler class.
112 bool EmitSuggestions);
113
114namespace internal {
115// Tests if any two `FixItHint`s in `FixIts` conflict. Two `FixItHint`s
116// conflict if they have overlapping source ranges.
118 const SourceManager &SM);
119} // namespace internal
120} // end namespace clang
121
122#endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */
#define SM(sm)
Definition: Cuda.cpp:80
StringRef Text
Definition: Format.cpp:2974
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
Definition: Stmt.h:84
The interface that lets the caller handle unsafe buffer usage analysis results by overriding this cla...
virtual void handleUnsafeOperation(const Stmt *Operation, bool IsRelatedToDecl)=0
Invoked when an unsafe operation over raw pointers is found.
void addDebugNoteForVar(const VarDecl *VD, SourceLocation Loc, std::string Text)
std::vector< DebugNote > DebugNoteList
virtual std::string getUnsafeBufferUsageAttributeTextAt(SourceLocation Loc, StringRef WSSuffix="") const =0
virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const =0
Returns a reference to the Preprocessor:
std::map< const VarDecl *, DebugNoteList > DebugNoteByVar
std::pair< SourceLocation, std::string > DebugNote
virtual void handleUnsafeVariableGroup(const VarDecl *Variable, const VariableGroupsManager &VarGrpMgr, FixItList &&Fixes, const Decl *D)=0
Invoked when a fix is suggested against a variable.
virtual ~UnsafeBufferUsageHandler()=default
Represents a variable declaration or definition.
Definition: Decl.h:916
virtual ~VariableGroupsManager()=default
virtual VarGrpRef getGroupOfVar(const VarDecl *Var, bool *HasParm=nullptr) const =0
Returns the set of variables (including Var) that need to be fixed together in one step.
virtual VarGrpRef getGroupOfParms() const =0
Returns the non-empty group of variables that include parameters of the analyzing function,...
bool anyConflict(const llvm::SmallVectorImpl< FixItHint > &FixIts, const SourceManager &SM)
void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler, bool EmitSuggestions)
std::vector< const VarDecl * > VarGrpTy