clang 17.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
20namespace clang {
21
22using 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.
27public:
29 virtual ~UnsafeBufferUsageHandler() = default;
30
31 /// This analyses produces large fixits that are organized into lists
32 /// of primitive fixits (individual insertions/removals/replacements).
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 getUserFillPlaceHolder(StringRef HintTextToUser = "placeholder") const {
52 std::string s = std::string("<# ");
53 s += HintTextToUser;
54 s += " #>";
55 return s;
56 }
57};
58
59// This function invokes the analysis and allows the caller to react to it
60// through the handler class.
61void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler,
62 bool EmitSuggestions);
63
64namespace internal {
65// Tests if any two `FixItHint`s in `FixIts` conflict. Two `FixItHint`s
66// conflict if they have overlapping source ranges.
68 const SourceManager &SM);
69} // namespace internal
70} // end namespace clang
71
72#endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */
#define SM(sm)
Definition: Cuda.cpp:80
__device__ __2f16 float bool s
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:72
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.
virtual void handleUnsafeVariableGroup(const VarDecl *Variable, const DefMapTy &VarGrpMap, FixItList &&Fixes)=0
Invoked when a fix is suggested against a variable.
virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const =0
Returns a reference to the Preprocessor:
virtual ~UnsafeBufferUsageHandler()=default
virtual std::string getUserFillPlaceHolder(StringRef HintTextToUser="placeholder") const
Returns the text indicating that the user needs to provide input there:
Represents a variable declaration or definition.
Definition: Decl.h:913
bool anyConflict(const llvm::SmallVectorImpl< FixItHint > &FixIts, const SourceManager &SM)
void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler, bool EmitSuggestions)
llvm::DenseMap< const VarDecl *, std::vector< const VarDecl * > > DefMapTy