Skip to content

Commit 8641687

Browse files
committed
Revert "Revert "[-Wunsafe-buffer-usage] Add a new forEachDescendant matcher that skips callable declarations""
This reverts commit f58b025. The previous revert reverts a patch that causes compilation problem on windows which can be reproduced using `-fdelayed-template-parsing`. I'm now to revert the patch back and commit a fix next.
1 parent b67c16f commit 8641687

File tree

2 files changed

+166
-5
lines changed

2 files changed

+166
-5
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,111 @@
88

99
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
1010
#include "clang/ASTMatchers/ASTMatchFinder.h"
11+
#include "clang/AST/RecursiveASTVisitor.h"
1112
#include "llvm/ADT/SmallVector.h"
1213

1314
using namespace llvm;
1415
using namespace clang;
1516
using namespace ast_matchers;
1617

18+
namespace clang::ast_matchers::internal {
19+
// A `RecursiveASTVisitor` that traverses all descendants of a given node "n"
20+
// except for those belonging to a different callable of "n".
21+
class MatchDescendantVisitor
22+
: public RecursiveASTVisitor<MatchDescendantVisitor> {
23+
public:
24+
typedef RecursiveASTVisitor<MatchDescendantVisitor> VisitorBase;
25+
26+
// Creates an AST visitor that matches `Matcher` on all
27+
// descendants of a given node "n" except for the ones
28+
// belonging to a different callable of "n".
29+
MatchDescendantVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder,
30+
BoundNodesTreeBuilder *Builder,
31+
ASTMatchFinder::BindKind Bind)
32+
: Matcher(Matcher), Finder(Finder), Builder(Builder), Bind(Bind),
33+
Matches(false) {}
34+
35+
// Returns true if a match is found in a subtree of `DynNode`, which belongs
36+
// to the same callable of `DynNode`.
37+
bool findMatch(const DynTypedNode &DynNode) {
38+
Matches = false;
39+
if (const Stmt *StmtNode = DynNode.get<Stmt>()) {
40+
TraverseStmt(const_cast<Stmt *>(StmtNode));
41+
*Builder = ResultBindings;
42+
return Matches;
43+
}
44+
return false;
45+
}
46+
47+
// The following are overriding methods from the base visitor class.
48+
// They are public only to allow CRTP to work. They are *not *part
49+
// of the public API of this class.
50+
51+
// For the matchers so far used in safe buffers, we only need to match
52+
// `Stmt`s. To override more as needed.
53+
54+
bool TraverseDecl(Decl *Node) {
55+
if (!Node)
56+
return true;
57+
if (!match(*Node))
58+
return false;
59+
// To skip callables:
60+
if (isa<FunctionDecl, BlockDecl, ObjCMethodDecl>(Node))
61+
return true;
62+
// Traverse descendants
63+
return VisitorBase::TraverseDecl(Node);
64+
}
65+
66+
bool TraverseStmt(Stmt *Node, DataRecursionQueue *Queue = nullptr) {
67+
if (!Node)
68+
return true;
69+
if (!match(*Node))
70+
return false;
71+
// To skip callables:
72+
if (isa<LambdaExpr>(Node))
73+
return true;
74+
return VisitorBase::TraverseStmt(Node);
75+
}
76+
77+
bool shouldVisitTemplateInstantiations() const { return true; }
78+
bool shouldVisitImplicitCode() const {
79+
// TODO: let's ignore implicit code for now
80+
return false;
81+
}
82+
83+
private:
84+
// Sets 'Matched' to true if 'Matcher' matches 'Node'
85+
//
86+
// Returns 'true' if traversal should continue after this function
87+
// returns, i.e. if no match is found or 'Bind' is 'BK_All'.
88+
template <typename T> bool match(const T &Node) {
89+
BoundNodesTreeBuilder RecursiveBuilder(*Builder);
90+
91+
if (Matcher->matches(DynTypedNode::create(Node), Finder,
92+
&RecursiveBuilder)) {
93+
ResultBindings.addMatch(RecursiveBuilder);
94+
Matches = true;
95+
if (Bind != ASTMatchFinder::BK_All)
96+
return false; // Abort as soon as a match is found.
97+
}
98+
return true;
99+
}
100+
101+
const DynTypedMatcher *const Matcher;
102+
ASTMatchFinder *const Finder;
103+
BoundNodesTreeBuilder *const Builder;
104+
BoundNodesTreeBuilder ResultBindings;
105+
const ASTMatchFinder::BindKind Bind;
106+
bool Matches;
107+
};
108+
109+
AST_MATCHER_P(Stmt, forEveryDescendant, Matcher<Stmt>, innerMatcher) {
110+
MatchDescendantVisitor Visitor(new DynTypedMatcher(innerMatcher), Finder,
111+
Builder, ASTMatchFinder::BK_All);
112+
return Visitor.findMatch(DynTypedNode::create(Node));
113+
}
114+
} // namespace clang::ast_matchers::internal
115+
17116
namespace {
18117
// Because the analysis revolves around variables and their types, we'll need to
19118
// track uses of variables (aka DeclRefExprs).
@@ -398,7 +497,7 @@ static std::pair<GadgetList, DeclUseTracker> findGadgets(const Decl *D) {
398497

399498
// clang-format off
400499
M.addMatcher(
401-
stmt(forEachDescendant(
500+
stmt(forEveryDescendant(
402501
stmt(anyOf(
403502
// Add Gadget::matcher() for every gadget in the registry.
404503
#define GADGET(x) \

clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -include %s -verify %s
1+
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fblocks -include %s -verify %s
22
#ifndef INCLUDED
33
#define INCLUDED
44
#pragma clang system_header
@@ -141,15 +141,15 @@ void testStructMembers(struct T * sp, struct T s, T_t * sp2, T_t s2) {
141141

142142
int garray[10];
143143
int * gp = garray;
144-
int gvar = gp[1]; // TODO: this is not warned
144+
int gvar = gp[1]; // FIXME: file scope unsafe buffer access is not warned
145145

146146
void testLambdaCaptureAndGlobal(int * p) {
147147
int a[10];
148148

149149
auto Lam = [p, a]() {
150-
return p[1] // expected-warning2{{unchecked operation on raw buffer in expression}}
150+
return p[1] // expected-warning{{unchecked operation on raw buffer in expression}}
151151
+ a[1] + garray[1]
152-
+ gp[1]; // expected-warning2{{unchecked operation on raw buffer in expression}}
152+
+ gp[1]; // expected-warning{{unchecked operation on raw buffer in expression}}
153153
};
154154
}
155155

@@ -213,4 +213,66 @@ void testPointerToMember() {
213213
foo(S.*p,
214214
(S.*q)[1]); // expected-warning{{unchecked operation on raw buffer in expression}}
215215
}
216+
217+
// test that nested callable definitions are scanned only once
218+
void testNestedCallableDefinition(int * p) {
219+
class A {
220+
void inner(int * p) {
221+
p++; // expected-warning{{unchecked operation on raw buffer in expression}}
222+
}
223+
224+
static void innerStatic(int * p) {
225+
p++; // expected-warning{{unchecked operation on raw buffer in expression}}
226+
}
227+
228+
void innerInner(int * p) {
229+
auto Lam = [p]() {
230+
int * q = p;
231+
q++; // expected-warning{{unchecked operation on raw buffer in expression}}
232+
return *q;
233+
};
234+
}
235+
};
236+
237+
auto Lam = [p]() {
238+
int * q = p;
239+
q++; // expected-warning{{unchecked operation on raw buffer in expression}}
240+
return *q;
241+
};
242+
243+
auto LamLam = [p]() {
244+
auto Lam = [p]() {
245+
int * q = p;
246+
q++; // expected-warning{{unchecked operation on raw buffer in expression}}
247+
return *q;
248+
};
249+
};
250+
251+
void (^Blk)(int*) = ^(int *p) {
252+
p++; // expected-warning{{unchecked operation on raw buffer in expression}}
253+
};
254+
255+
void (^BlkBlk)(int*) = ^(int *p) {
256+
void (^Blk)(int*) = ^(int *p) {
257+
p++; // expected-warning{{unchecked operation on raw buffer in expression}}
258+
};
259+
Blk(p);
260+
};
261+
262+
// lambda and block as call arguments...
263+
foo( [p]() { int * q = p;
264+
q++; // expected-warning{{unchecked operation on raw buffer in expression}}
265+
return *q;
266+
},
267+
^(int *p) { p++; // expected-warning{{unchecked operation on raw buffer in expression}}
268+
}
269+
);
270+
}
271+
272+
void testVariableDecls(int * p) {
273+
int * q = p++; // expected-warning{{unchecked operation on raw buffer in expression}}
274+
int a[p[1]]; // expected-warning{{unchecked operation on raw buffer in expression}}
275+
int b = p[1]; // expected-warning{{unchecked operation on raw buffer in expression}}
276+
}
277+
216278
#endif

0 commit comments

Comments
 (0)