Skip to content

Commit 0d8bb00

Browse files
author
z1.cciauto
committed
merge main into amd-staging
2 parents e802dc2 + 357e380 commit 0d8bb00

File tree

105 files changed

+5666
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+5666
-224
lines changed

clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/Stmt.h"
2020
#include "clang/Basic/SourceLocation.h"
2121
#include "llvm/Support/Debug.h"
22+
#include <set>
2223

2324
namespace clang {
2425

@@ -186,6 +187,8 @@ namespace internal {
186187
bool anyConflict(const llvm::SmallVectorImpl<FixItHint> &FixIts,
187188
const SourceManager &SM);
188189
} // namespace internal
190+
191+
std::set<const Expr *> findUnsafePointers(const FunctionDecl *FD);
189192
} // end namespace clang
190193

191194
#endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */

clang/lib/AST/ASTImporter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8931,14 +8931,14 @@ ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
89318931
SubstNonTypeTemplateParmExpr *E) {
89328932
Error Err = Error::success();
89338933
auto ToType = importChecked(Err, E->getType());
8934-
auto ToExprLoc = importChecked(Err, E->getExprLoc());
8934+
auto ToNameLoc = importChecked(Err, E->getNameLoc());
89358935
auto ToAssociatedDecl = importChecked(Err, E->getAssociatedDecl());
89368936
auto ToReplacement = importChecked(Err, E->getReplacement());
89378937
if (Err)
89388938
return std::move(Err);
89398939

89408940
return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
8941-
ToType, E->getValueKind(), ToExprLoc, ToReplacement, ToAssociatedDecl,
8941+
ToType, E->getValueKind(), ToNameLoc, ToReplacement, ToAssociatedDecl,
89428942
E->getIndex(), E->getPackIndex(), E->isReferenceParameter(),
89438943
E->getFinal());
89448944
}

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,8 @@ class WarningGadget : public Gadget {
11631163
virtual void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,
11641164
bool IsRelatedToDecl,
11651165
ASTContext &Ctx) const = 0;
1166+
1167+
virtual SmallVector<const Expr *, 1> getUnsafePtrs() const = 0;
11661168
};
11671169

11681170
/// Fixable gadgets correspond to code patterns that aren't always unsafe but
@@ -1245,6 +1247,10 @@ class IncrementGadget : public WarningGadget {
12451247

12461248
return std::move(Uses);
12471249
}
1250+
1251+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1252+
return {Op->getSubExpr()->IgnoreParenImpCasts()};
1253+
}
12481254
};
12491255

12501256
/// A decrement of a pointer-type value is unsafe as it may run the pointer
@@ -1288,6 +1294,10 @@ class DecrementGadget : public WarningGadget {
12881294

12891295
return {};
12901296
}
1297+
1298+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1299+
return {Op->getSubExpr()->IgnoreParenImpCasts()};
1300+
}
12911301
};
12921302

12931303
/// Array subscript expressions on raw pointers as if they're arrays. Unsafe as
@@ -1337,6 +1347,10 @@ class ArraySubscriptGadget : public WarningGadget {
13371347

13381348
return {};
13391349
}
1350+
1351+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1352+
return {ASE->getBase()->IgnoreParenImpCasts()};
1353+
}
13401354
};
13411355

13421356
/// A pointer arithmetic expression of one of the forms:
@@ -1400,6 +1414,11 @@ class PointerArithmeticGadget : public WarningGadget {
14001414

14011415
return {};
14021416
}
1417+
1418+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1419+
return {Ptr->IgnoreParenImpCasts()};
1420+
}
1421+
14031422
// FIXME: pointer adding zero should be fine
14041423
// FIXME: this gadge will need a fix-it
14051424
};
@@ -1457,6 +1476,8 @@ class SpanTwoParamConstructorGadget : public WarningGadget {
14571476
}
14581477
return {};
14591478
}
1479+
1480+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
14601481
};
14611482

14621483
/// A pointer initialization expression of the form:
@@ -1689,6 +1710,8 @@ class UnsafeBufferUsageAttrGadget : public WarningGadget {
16891710
SourceLocation getSourceLoc() const override { return Op->getBeginLoc(); }
16901711

16911712
DeclUseList getClaimedVarUseSites() const override { return {}; }
1713+
1714+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
16921715
};
16931716

16941717
/// A call of a constructor that performs unchecked buffer operations
@@ -1727,6 +1750,8 @@ class UnsafeBufferUsageCtorAttrGadget : public WarningGadget {
17271750
SourceLocation getSourceLoc() const override { return Op->getBeginLoc(); }
17281751

17291752
DeclUseList getClaimedVarUseSites() const override { return {}; }
1753+
1754+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
17301755
};
17311756

17321757
// Warning gadget for unsafe invocation of span::data method.
@@ -1793,6 +1818,8 @@ class DataInvocationGadget : public WarningGadget {
17931818
return true;
17941819
return false;
17951820
}
1821+
1822+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
17961823
};
17971824

17981825
class UnsafeLibcFunctionCallGadget : public WarningGadget {
@@ -1896,6 +1923,8 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
18961923
}
18971924

18981925
DeclUseList getClaimedVarUseSites() const override { return {}; }
1926+
1927+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
18991928
};
19001929

19011930
// Represents expressions of the form `DRE[*]` in the Unspecified Lvalue
@@ -2467,6 +2496,52 @@ template <typename NodeTy> struct CompareNode {
24672496
}
24682497
};
24692498

2499+
std::set<const Expr *> clang::findUnsafePointers(const FunctionDecl *FD) {
2500+
class MockReporter : public UnsafeBufferUsageHandler {
2501+
public:
2502+
MockReporter() {}
2503+
void handleUnsafeOperation(const Stmt *, bool, ASTContext &) override {}
2504+
void handleUnsafeLibcCall(const CallExpr *, unsigned, ASTContext &,
2505+
const Expr *UnsafeArg = nullptr) override {}
2506+
void handleUnsafeOperationInContainer(const Stmt *, bool,
2507+
ASTContext &) override {}
2508+
void handleUnsafeVariableGroup(const VarDecl *,
2509+
const VariableGroupsManager &, FixItList &&,
2510+
const Decl *,
2511+
const FixitStrategy &) override {}
2512+
bool isSafeBufferOptOut(const SourceLocation &) const override {
2513+
return false;
2514+
}
2515+
bool ignoreUnsafeBufferInContainer(const SourceLocation &) const override {
2516+
return false;
2517+
}
2518+
bool ignoreUnsafeBufferInLibcCall(const SourceLocation &) const override {
2519+
return false;
2520+
}
2521+
std::string getUnsafeBufferUsageAttributeTextAt(
2522+
SourceLocation, StringRef WSSuffix = "") const override {
2523+
return "";
2524+
}
2525+
};
2526+
2527+
FixableGadgetList FixableGadgets;
2528+
WarningGadgetList WarningGadgets;
2529+
DeclUseTracker Tracker;
2530+
MockReporter IgnoreHandler;
2531+
2532+
findGadgets(FD->getBody(), FD->getASTContext(), IgnoreHandler, false,
2533+
FixableGadgets, WarningGadgets, Tracker);
2534+
2535+
std::set<const Expr *> Result;
2536+
for (auto &G : WarningGadgets) {
2537+
for (const Expr *E : G->getUnsafePtrs()) {
2538+
Result.insert(E);
2539+
}
2540+
}
2541+
2542+
return Result;
2543+
}
2544+
24702545
struct WarningGadgetSets {
24712546
std::map<const VarDecl *, std::set<const WarningGadget *>,
24722547
// To keep keys sorted by their locations in the map so that the

clang/lib/Sema/SemaConcept.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,11 +1929,11 @@ auto SubsumptionChecker::find(AtomicConstraint *Ori) -> Literal {
19291929
}
19301930
auto It = Elems.find(ID);
19311931
if (It == Elems.end()) {
1932-
It =
1933-
Elems
1934-
.insert({ID, MappedAtomicConstraint{Ori, Literal{getNewLiteralId(),
1935-
Literal::Atomic}}})
1936-
.first;
1932+
It = Elems
1933+
.insert({ID,
1934+
MappedAtomicConstraint{
1935+
Ori, {getNewLiteralId(), Literal::Atomic}}})
1936+
.first;
19371937
ReverseMap[It->second.ID.Value] = Ori;
19381938
}
19391939
return It->getSecond().ID;

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,10 +2226,7 @@ void ASTStmtReader::VisitSubstNonTypeTemplateParmExpr(
22262226
E->AssociatedDeclAndRef.setPointer(readDeclAs<Decl>());
22272227
E->AssociatedDeclAndRef.setInt(CurrentUnpackingBits->getNextBit());
22282228
E->Index = CurrentUnpackingBits->getNextBits(/*Width=*/12);
2229-
if (CurrentUnpackingBits->getNextBit())
2230-
E->PackIndex = Record.readInt();
2231-
else
2232-
E->PackIndex = 0;
2229+
E->PackIndex = Record.readUnsignedOrNone().toInternalRepresentation();
22332230
E->Final = CurrentUnpackingBits->getNextBit();
22342231
E->SubstNonTypeTemplateParmExprBits.NameLoc = readSourceLocation();
22352232
E->Replacement = Record.readSubExpr();
@@ -2239,6 +2236,7 @@ void ASTStmtReader::VisitSubstNonTypeTemplateParmPackExpr(
22392236
SubstNonTypeTemplateParmPackExpr *E) {
22402237
VisitExpr(E);
22412238
E->AssociatedDecl = readDeclAs<Decl>();
2239+
E->Final = CurrentUnpackingBits->getNextBit();
22422240
E->Index = Record.readInt();
22432241
TemplateArgument ArgPack = Record.readTemplateArgument();
22442242
if (ArgPack.getKind() != TemplateArgument::Pack)

clang/lib/Serialization/ASTWriterStmt.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,9 +2228,7 @@ void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
22282228
Record.AddDeclRef(E->getAssociatedDecl());
22292229
CurrentPackingBits.addBit(E->isReferenceParameter());
22302230
CurrentPackingBits.addBits(E->getIndex(), /*Width=*/12);
2231-
CurrentPackingBits.addBit((bool)E->getPackIndex());
2232-
if (auto PackIndex = E->getPackIndex())
2233-
Record.push_back(*PackIndex + 1);
2231+
Record.writeUnsignedOrNone(E->getPackIndex());
22342232
CurrentPackingBits.addBit(E->getFinal());
22352233

22362234
Record.AddSourceLocation(E->getNameLoc());
@@ -2242,6 +2240,7 @@ void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
22422240
SubstNonTypeTemplateParmPackExpr *E) {
22432241
VisitExpr(E);
22442242
Record.AddDeclRef(E->getAssociatedDecl());
2243+
CurrentPackingBits.addBit(E->getFinal());
22452244
Record.push_back(E->getIndex());
22462245
Record.AddTemplateArgument(E->getArgumentPack());
22472246
Record.AddSourceLocation(E->getParameterPackLocation());

0 commit comments

Comments
 (0)