Skip to content

Commit 9aa00c8

Browse files
committed
Revert "[-Wunsafe-buffer-usage] Add unsafe buffer checking opt-out pragmas"
This reverts commit aef05b5. It causes a buildbot failure: https://lab.llvm.org/buildbot/#/builders/216/builds/16879/steps/6/logs/stdio
1 parent 16a1c85 commit 9aa00c8

12 files changed

+3
-435
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class UnsafeBufferUsageHandler {
4646
s += " #>";
4747
return s;
4848
}
49-
50-
/// Returns a reference to the `Preprocessor`:
51-
virtual const Preprocessor & getPP() const;
5249
};
5350

5451
// This function invokes the analysis and allows the caller to react to it

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -945,15 +945,4 @@ def err_dep_source_scanner_unexpected_tokens_at_import : Error<
945945

946946
}
947947

948-
def err_pp_double_begin_pragma_unsafe_buffer_usage :
949-
Error<"already inside '#pragma unsafe_buffer_usage'">;
950-
951-
def err_pp_unmatched_end_begin_pragma_unsafe_buffer_usage :
952-
Error<"not currently inside '#pragma unsafe_buffer_usage'">;
953-
954-
def err_pp_unclosed_pragma_unsafe_buffer_usage :
955-
Error<"'#pragma unsafe_buffer_usage' was not ended">;
956-
957-
def err_pp_pragma_unsafe_buffer_usage_syntax :
958-
Error<"Expected 'begin' or 'end'">;
959948
}

clang/include/clang/Lex/Preprocessor.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,51 +2688,6 @@ class Preprocessor {
26882688
void emitMacroDeprecationWarning(const Token &Identifier) const;
26892689
void emitRestrictExpansionWarning(const Token &Identifier) const;
26902690
void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const;
2691-
2692-
/// This boolean state keeps track if the current scanned token (by this PP)
2693-
/// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a
2694-
/// translation unit in a linear order.
2695-
bool InSafeBufferOptOutRegion = 0;
2696-
2697-
/// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out
2698-
/// region if PP is currently in such a region. Hold undefined value
2699-
/// otherwise.
2700-
SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region.
2701-
2702-
// An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in one
2703-
// translation unit. Each region is represented by a pair of start and end
2704-
// locations. A region is "open" if its' start and end locations are
2705-
// identical.
2706-
SmallVector<std::pair<SourceLocation, SourceLocation>, 8> SafeBufferOptOutMap;
2707-
2708-
public:
2709-
/// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out
2710-
/// region. This `Loc` must be a source location that has been pre-processed.
2711-
bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const;
2712-
2713-
/// Alter the state of whether this PP currently is in a
2714-
/// "-Wunsafe-buffer-usage" opt-out region.
2715-
///
2716-
/// \param isEnter: true if this PP is entering a region; otherwise, this PP
2717-
/// is exiting a region
2718-
/// \param Loc: the location of the entry or exit of a
2719-
/// region
2720-
/// \return true iff it is INVALID to enter or exit a region, i.e.,
2721-
/// attempt to enter a region before exiting a previous region, or exiting a
2722-
/// region that PP is not currently in.
2723-
bool enterOrExitSafeBufferOptOutRegion(bool isEnter,
2724-
const SourceLocation &Loc);
2725-
2726-
/// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2727-
/// opt-out region
2728-
bool isPPInSafeBufferOptOutRegion();
2729-
2730-
/// \param StartLoc: output argument. It will be set to the start location of
2731-
/// the current "-Wunsafe-buffer-usage" opt-out region iff this function
2732-
/// returns true.
2733-
/// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2734-
/// opt-out region
2735-
bool isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc);
27362691
};
27372692

27382693
/// Abstract base class that describes a handler that will receive

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
1010
#include "clang/AST/RecursiveASTVisitor.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12-
#include "clang/Lex/Preprocessor.h"
1312
#include "clang/Lex/Lexer.h"
1413
#include "llvm/ADT/SmallVector.h"
1514
#include <memory>
@@ -118,12 +117,6 @@ AST_MATCHER_P(Stmt, forEveryDescendant, internal::Matcher<Stmt>, innerMatcher) {
118117
return Visitor.findMatch(DynTypedNode::create(Node));
119118
}
120119

121-
// Matches a `Stmt` node iff the node is in a safe-buffer opt-out region
122-
AST_MATCHER_P(Stmt, notInSafeBufferOptOut, const Preprocessor *, PP) {
123-
const SourceManager &SM = Finder->getASTContext().getSourceManager();
124-
return !PP->isSafeBufferOptOut(SM, Node.getBeginLoc());
125-
}
126-
127120
AST_MATCHER_P(CastExpr, castSubExpr, internal::Matcher<Expr>, innerMatcher) {
128121
return innerMatcher.matches(*Node.getSubExpr(), Finder, Builder);
129122
}
@@ -555,8 +548,7 @@ class Strategy {
555548
} // namespace
556549

557550
/// Scan the function and return a list of gadgets found with provided kits.
558-
static std::tuple<FixableGadgetList, WarningGadgetList, DeclUseTracker>
559-
findGadgets(const Decl *D, const Preprocessor &PP) {
551+
static std::tuple<FixableGadgetList, WarningGadgetList, DeclUseTracker> findGadgets(const Decl *D) {
560552

561553
struct GadgetFinderCallback : MatchFinder::MatchCallback {
562554
FixableGadgetList FixableGadgets;
@@ -628,7 +620,7 @@ findGadgets(const Decl *D, const Preprocessor &PP) {
628620
stmt(anyOf(
629621
// Add Gadget::matcher() for every gadget in the registry.
630622
#define WARNING_GADGET(x) \
631-
allOf(x ## Gadget::matcher().bind(#x), notInSafeBufferOptOut(&PP)),
623+
x ## Gadget::matcher().bind(#x),
632624
#include "clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def"
633625
// In parallel, match all DeclRefExprs so that to find out
634626
// whether there are any uncovered by gadgets.
@@ -1017,7 +1009,7 @@ void clang::checkUnsafeBufferUsage(const Decl *D,
10171009
DeclUseTracker Tracker;
10181010

10191011
{
1020-
auto [FixableGadgets, WarningGadgets, TrackerRes] = findGadgets(D, Handler.getPP());
1012+
auto [FixableGadgets, WarningGadgets, TrackerRes] = findGadgets(D);
10211013
UnsafeOps = groupWarningGadgetsByVar(std::move(WarningGadgets));
10221014
FixablesForUnsafeVars = groupFixablesByVar(std::move(FixableGadgets));
10231015
Tracker = std::move(TrackerRes);

clang/lib/Lex/PPLexerChange.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,6 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
333333
assert(!CurTokenLexer &&
334334
"Ending a file when currently in a macro!");
335335

336-
SourceLocation UnclosedSafeBufferOptOutLoc;
337-
338-
if (IncludeMacroStack.empty() &&
339-
isPPInSafeBufferOptOutRegion(UnclosedSafeBufferOptOutLoc)) {
340-
// To warn if a "-Wunsafe-buffer-usage" opt-out region is still open by the
341-
// end of a file.
342-
Diag(UnclosedSafeBufferOptOutLoc,
343-
diag::err_pp_unclosed_pragma_unsafe_buffer_usage);
344-
}
345336
// If we have an unclosed module region from a pragma at the end of a
346337
// module, complain and close it now.
347338
const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;

clang/lib/Lex/Pragma.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,32 +1243,6 @@ struct PragmaDebugHandler : public PragmaHandler {
12431243
#endif
12441244
};
12451245

1246-
struct PragmaUnsafeBufferUsageHandler : public PragmaHandler {
1247-
PragmaUnsafeBufferUsageHandler() : PragmaHandler("unsafe_buffer_usage") {}
1248-
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1249-
Token &FirstToken) override {
1250-
Token Tok;
1251-
1252-
PP.LexUnexpandedToken(Tok);
1253-
if (Tok.isNot(tok::identifier)) {
1254-
PP.Diag(Tok, diag::err_pp_pragma_unsafe_buffer_usage_syntax);
1255-
return;
1256-
}
1257-
1258-
IdentifierInfo *II = Tok.getIdentifierInfo();
1259-
SourceLocation Loc = Tok.getLocation();
1260-
1261-
if (II->isStr("begin")) {
1262-
if (PP.enterOrExitSafeBufferOptOutRegion(true, Loc))
1263-
PP.Diag(Loc, diag::err_pp_double_begin_pragma_unsafe_buffer_usage);
1264-
} else if (II->isStr("end")) {
1265-
if (PP.enterOrExitSafeBufferOptOutRegion(false, Loc))
1266-
PP.Diag(Loc, diag::err_pp_unmatched_end_begin_pragma_unsafe_buffer_usage);
1267-
} else
1268-
PP.Diag(Tok, diag::err_pp_pragma_unsafe_buffer_usage_syntax);
1269-
}
1270-
};
1271-
12721246
/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
12731247
struct PragmaDiagnosticHandler : public PragmaHandler {
12741248
private:
@@ -2154,9 +2128,6 @@ void Preprocessor::RegisterBuiltinPragmas() {
21542128
ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
21552129
ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
21562130

2157-
// Safe Buffers pragmas
2158-
AddPragmaHandler("clang", new PragmaUnsafeBufferUsageHandler);
2159-
21602131
// Add region pragmas.
21612132
AddPragmaHandler(new PragmaRegionHandler("region"));
21622133
AddPragmaHandler(new PragmaRegionHandler("endregion"));

clang/lib/Lex/Preprocessor.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,75 +1462,6 @@ void Preprocessor::emitFinalMacroWarning(const Token &Identifier,
14621462
Diag(*A.FinalAnnotationLoc, diag::note_pp_macro_annotation) << 2;
14631463
}
14641464

1465-
bool Preprocessor::isSafeBufferOptOut(const SourceManager &SourceMgr,
1466-
const SourceLocation &Loc) const {
1467-
// Try to find a region in `SafeBufferOptOutMap` where `Loc` is in:
1468-
auto FirstRegionEndingAfterLoc = llvm::partition_point(
1469-
SafeBufferOptOutMap,
1470-
[&SourceMgr,
1471-
&Loc](const std::pair<SourceLocation, SourceLocation> &Region) {
1472-
return SourceMgr.isBeforeInTranslationUnit(Region.second, Loc);
1473-
});
1474-
1475-
if (FirstRegionEndingAfterLoc != SafeBufferOptOutMap.end()) {
1476-
// To test if the start location of the found region precedes `Loc`:
1477-
return SourceMgr.isBeforeInTranslationUnit(FirstRegionEndingAfterLoc->first,
1478-
Loc);
1479-
}
1480-
// If we do not find a region whose end location passes `Loc`, we want to
1481-
// check if the current region is still open:
1482-
if (!SafeBufferOptOutMap.empty() &&
1483-
SafeBufferOptOutMap.back().first == SafeBufferOptOutMap.back().second)
1484-
return SourceMgr.isBeforeInTranslationUnit(SafeBufferOptOutMap.back().first,
1485-
Loc);
1486-
return false;
1487-
}
1488-
1489-
bool Preprocessor::enterOrExitSafeBufferOptOutRegion(
1490-
bool isEnter, const SourceLocation &Loc) {
1491-
if (isEnter) {
1492-
if (isPPInSafeBufferOptOutRegion())
1493-
return true; // invalid enter action
1494-
InSafeBufferOptOutRegion = true;
1495-
CurrentSafeBufferOptOutStart = Loc;
1496-
1497-
// To set the start location of a new region:
1498-
1499-
if (!SafeBufferOptOutMap.empty()) {
1500-
auto *PrevRegion = &SafeBufferOptOutMap.back();
1501-
assert(PrevRegion->first != PrevRegion->second &&
1502-
"Shall not begin a safe buffer opt-out region before closing the "
1503-
"previous one.");
1504-
}
1505-
// If the start location equals to the end location, we call the region a
1506-
// open region or a unclosed region (i.e., end location has not been set
1507-
// yet).
1508-
SafeBufferOptOutMap.emplace_back(Loc, Loc);
1509-
} else {
1510-
if (!isPPInSafeBufferOptOutRegion())
1511-
return true; // invalid enter action
1512-
InSafeBufferOptOutRegion = false;
1513-
1514-
// To set the end location of the current open region:
1515-
1516-
assert(!SafeBufferOptOutMap.empty() &&
1517-
"Misordered safe buffer opt-out regions");
1518-
auto *CurrRegion = &SafeBufferOptOutMap.back();
1519-
assert(CurrRegion->first == CurrRegion->second &&
1520-
"Set end location to a closed safe buffer opt-out region");
1521-
CurrRegion->second = Loc;
1522-
}
1523-
return false;
1524-
}
1525-
1526-
bool Preprocessor::isPPInSafeBufferOptOutRegion() {
1527-
return InSafeBufferOptOutRegion;
1528-
}
1529-
bool Preprocessor::isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc) {
1530-
StartLoc = CurrentSafeBufferOptOutStart;
1531-
return InSafeBufferOptOutRegion;
1532-
}
1533-
15341465
ModuleLoader::~ModuleLoader() = default;
15351466

15361467
CommentHandler::~CommentHandler() = default;

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,10 +2213,6 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
22132213
FD << F;
22142214
}
22152215
}
2216-
2217-
const clang::Preprocessor & getPP() const override {
2218-
return S.getPreprocessor();
2219-
}
22202216
};
22212217
} // namespace
22222218

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

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)