[Analysis] Use std::nullopt instead of None (NFC)

This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
This commit is contained in:
Kazu Hirata 2022-12-03 11:34:25 -08:00
parent 180600660b
commit 34e0d0579a
14 changed files with 44 additions and 43 deletions

View File

@ -54,11 +54,12 @@ public:
// occasionally hand out null pointers for pruned edges, so we catch those
// here.
if (!Block)
return std::make_pair(None, false); // if an edge is trivially false.
return std::make_pair(std::nullopt,
false); // if an edge is trivially false.
if (VisitedBlockIDs.test(Block->getBlockID()))
return std::make_pair(None, false);
return std::make_pair(std::nullopt, false);
VisitedBlockIDs.set(Block->getBlockID());
return std::make_pair(None, true);
return std::make_pair(std::nullopt, true);
}
/// Check if the bit for a CFGBlock has been already set.

View File

@ -1464,7 +1464,7 @@ public:
static bool classof(const SExpr *E) { return E->opcode() == COP_Return; }
/// Return an empty list.
ArrayRef<BasicBlock *> successors() { return None; }
ArrayRef<BasicBlock *> successors() { return std::nullopt; }
SExpr *returnValue() { return Retval; }
const SExpr *returnValue() const { return Retval; }
@ -1490,7 +1490,7 @@ inline ArrayRef<BasicBlock*> Terminator::successors() {
case COP_Branch: return cast<Branch>(this)->successors();
case COP_Return: return cast<Return>(this)->successors();
default:
return None;
return std::nullopt;
}
}

View File

@ -123,7 +123,7 @@ public:
} else if (const auto *CXCIE = dyn_cast<CXXInheritedCtorInitExpr>(E)) {
return AnyCall(CXCIE);
} else {
return None;
return std::nullopt;
}
}
@ -136,13 +136,13 @@ public:
} else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
return AnyCall(MD);
}
return None;
return std::nullopt;
}
/// \returns formal parameters for direct calls (including virtual calls)
ArrayRef<ParmVarDecl *> parameters() const {
if (!D)
return None;
return std::nullopt;
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
return FD->parameters();
@ -151,7 +151,7 @@ public:
} else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
return BD->parameters();
} else {
return None;
return std::nullopt;
}
}

View File

@ -108,7 +108,7 @@ public:
template<typename T>
Optional<T> getAs() const {
if (!T::isKind(*this))
return None;
return std::nullopt;
T t;
CFGElement& e = t;
e = *this;

View File

@ -149,7 +149,7 @@ public:
template<typename T>
Optional<T> getAs() const {
if (!T::isKind(*this))
return None;
return std::nullopt;
T t;
ProgramPoint& PP = t;
PP = *this;

View File

@ -541,7 +541,7 @@ static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
CallExpr *CE = CallExpr::Create(
/*ASTContext=*/C,
/*StmtClass=*/M.makeLvalueToRvalue(/*Expr=*/Block),
/*Args=*/None,
/*Args=*/std::nullopt,
/*QualType=*/C.VoidTy,
/*ExprValueType=*/VK_PRValue,
/*SourceLocation=*/SourceLocation(), FPOptionsOverride());
@ -609,7 +609,7 @@ static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
ASTMaker M(C);
DeclRefExpr *DR = M.makeDeclRefExpr(PV);
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
CallExpr *CE = CallExpr::Create(C, ICE, None, C.VoidTy, VK_PRValue,
CallExpr *CE = CallExpr::Create(C, ICE, std::nullopt, C.VoidTy, VK_PRValue,
SourceLocation(), FPOptionsOverride());
return CE;
}

View File

@ -1044,14 +1044,14 @@ private:
return llvm::APInt(Context->getTypeSize(Context->IntTy), !Value);
default:
assert(false && "Unexpected unary operator!");
return llvm::None;
return std::nullopt;
}
}
} else if (const auto *IntLiteral =
dyn_cast<IntegerLiteral>(E->IgnoreParens()))
return IntLiteral->getValue();
return llvm::None;
return std::nullopt;
}
TryResult analyzeLogicOperatorCondition(BinaryOperatorKind Relation,

View File

@ -513,7 +513,7 @@ public:
if (const Stmt *Terminator = Conditional->getTerminatorStmt()) {
return NotCalledClarifier{Conditional, SuccWithoutCall}.Visit(Terminator);
}
return llvm::None;
return std::nullopt;
}
llvm::Optional<Clarification> VisitIfStmt(const IfStmt *If) {
@ -563,7 +563,7 @@ public:
llvm::Optional<Clarification> VisitBinaryOperator(const BinaryOperator *) {
// We don't want to report on short-curcuit logical operations.
return llvm::None;
return std::nullopt;
}
llvm::Optional<Clarification> VisitStmt(const Stmt *Terminator) {
@ -1008,7 +1008,7 @@ private:
return A->getCompletionHandlerIndex().getASTIndex() == ParamIndex;
}
return llvm::None;
return std::nullopt;
}
/// Return true if the specified selector represents init method.
@ -1644,7 +1644,7 @@ private:
return getIndex(*Parameter);
}
return llvm::None;
return std::nullopt;
}
llvm::Optional<unsigned> getIndex(const ParmVarDecl &Parameter) const {
@ -1662,7 +1662,7 @@ private:
return It - TrackedParams.begin();
}
return llvm::None;
return std::nullopt;
}
const ParmVarDecl *getParameter(unsigned Index) const {

View File

@ -56,7 +56,7 @@ auto hasOptionalType() { return hasType(optionalOrAliasType()); }
auto isOptionalMemberCallWithName(
llvm::StringRef MemberName,
llvm::Optional<StatementMatcher> Ignorable = llvm::None) {
llvm::Optional<StatementMatcher> Ignorable = std::nullopt) {
auto Exception = unless(Ignorable ? expr(anyOf(*Ignorable, cxxThisExpr()))
: cxxThisExpr());
return cxxMemberCallExpr(
@ -66,7 +66,7 @@ auto isOptionalMemberCallWithName(
auto isOptionalOperatorCallWithName(
llvm::StringRef operator_name,
llvm::Optional<StatementMatcher> Ignorable = llvm::None) {
llvm::Optional<StatementMatcher> Ignorable = std::nullopt) {
return cxxOperatorCallExpr(
hasOverloadedOperatorName(operator_name),
callee(cxxMethodDecl(ofClass(optionalClass()))),
@ -540,7 +540,7 @@ ignorableOptional(const UncheckedOptionalAccessModelOptions &Options) {
cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("->"),
hasOverloadedOperatorName("*")),
unless(hasArgument(0, expr(hasOptionalType())))))));
return llvm::None;
return std::nullopt;
}
StatementMatcher

View File

@ -414,7 +414,7 @@ runTypeErasedDataflowAnalysis(
ForwardDataflowWorklist Worklist(CFCtx.getCFG(), &POV);
std::vector<llvm::Optional<TypeErasedDataflowAnalysisState>> BlockStates(
CFCtx.getCFG().size(), llvm::None);
CFCtx.getCFG().size(), std::nullopt);
// The entry basic block doesn't contain statements so it can be skipped.
const CFGBlock &Entry = CFCtx.getCFG().getEntry();

View File

@ -99,11 +99,11 @@ void MacroExpansionContext::registerForPreprocessor(Preprocessor &NewPP) {
Optional<StringRef>
MacroExpansionContext::getExpandedText(SourceLocation MacroExpansionLoc) const {
if (MacroExpansionLoc.isMacroID())
return llvm::None;
return std::nullopt;
// If there was no macro expansion at that location, return None.
if (ExpansionRanges.find_as(MacroExpansionLoc) == ExpansionRanges.end())
return llvm::None;
return std::nullopt;
// There was macro expansion, but resulted in no tokens, return empty string.
const auto It = ExpandedTokens.find_as(MacroExpansionLoc);
@ -117,11 +117,11 @@ MacroExpansionContext::getExpandedText(SourceLocation MacroExpansionLoc) const {
Optional<StringRef>
MacroExpansionContext::getOriginalText(SourceLocation MacroExpansionLoc) const {
if (MacroExpansionLoc.isMacroID())
return llvm::None;
return std::nullopt;
const auto It = ExpansionRanges.find_as(MacroExpansionLoc);
if (It == ExpansionRanges.end())
return llvm::None;
return std::nullopt;
assert(It->getFirst() != It->getSecond() &&
"Every macro expansion must cover a non-empty range.");

View File

@ -239,7 +239,7 @@ compareControlFlow(const PathDiagnosticControlFlowPiece &X,
FullSourceLoc YEL = Y.getEndLocation().asLocation();
if (XEL != YEL)
return XEL.isBeforeInTranslationUnitThan(YEL);
return None;
return std::nullopt;
}
static Optional<bool> compareMacro(const PathDiagnosticMacroPiece &X,
@ -305,7 +305,7 @@ static Optional<bool> comparePiece(const PathDiagnosticPiece &X,
case PathDiagnosticPiece::Event:
case PathDiagnosticPiece::Note:
case PathDiagnosticPiece::PopUp:
return None;
return std::nullopt;
}
llvm_unreachable("all cases handled");
}
@ -323,7 +323,7 @@ static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y) {
return b.value();
}
return None;
return std::nullopt;
}
static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
@ -367,7 +367,7 @@ static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) {
return X.getShortDescription() < Y.getShortDescription();
auto CompareDecls = [&XL](const Decl *D1, const Decl *D2) -> Optional<bool> {
if (D1 == D2)
return None;
return std::nullopt;
if (!D1)
return true;
if (!D2)
@ -379,7 +379,7 @@ static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) {
return compareCrossTUSourceLocs(FullSourceLoc(D1L, SM),
FullSourceLoc(D2L, SM));
}
return None;
return std::nullopt;
};
if (auto Result = CompareDecls(X.getDeclWithIssue(), Y.getDeclWithIssue()))
return *Result;

View File

@ -71,7 +71,7 @@ Optional<ObjKind> RetainSummaryManager::hasAnyEnabledAttrOf(const Decl *D,
if (isOneOf<T, CFConsumedAttr, CFReturnsRetainedAttr,
CFReturnsNotRetainedAttr>()) {
if (!TrackObjCAndCFObjects)
return None;
return std::nullopt;
K = ObjKind::CF;
} else if (isOneOf<T, NSConsumedAttr, NSConsumesSelfAttr,
@ -79,19 +79,19 @@ Optional<ObjKind> RetainSummaryManager::hasAnyEnabledAttrOf(const Decl *D,
NSReturnsNotRetainedAttr, NSConsumesSelfAttr>()) {
if (!TrackObjCAndCFObjects)
return None;
return std::nullopt;
if (isOneOf<T, NSReturnsRetainedAttr, NSReturnsAutoreleasedAttr,
NSReturnsNotRetainedAttr>() &&
!cocoa::isCocoaObjectRef(QT))
return None;
return std::nullopt;
K = ObjKind::ObjC;
} else if (isOneOf<T, OSConsumedAttr, OSConsumesThisAttr,
OSReturnsNotRetainedAttr, OSReturnsRetainedAttr,
OSReturnsRetainedOnZeroAttr,
OSReturnsRetainedOnNonZeroAttr>()) {
if (!TrackOSObjects)
return None;
return std::nullopt;
K = ObjKind::OS;
} else if (isOneOf<T, GeneralizedReturnsNotRetainedAttr,
GeneralizedReturnsRetainedAttr,
@ -102,7 +102,7 @@ Optional<ObjKind> RetainSummaryManager::hasAnyEnabledAttrOf(const Decl *D,
}
if (D->hasAttr<T>())
return K;
return None;
return std::nullopt;
}
template <class T1, class T2, class... Others>
@ -724,7 +724,7 @@ RetainSummaryManager::canEval(const CallExpr *CE, const FunctionDecl *FD,
IdentifierInfo *II = FD->getIdentifier();
if (!II)
return None;
return std::nullopt;
StringRef FName = II->getName();
FName = FName.substr(FName.find_first_not_of('_'));
@ -741,7 +741,7 @@ RetainSummaryManager::canEval(const CallExpr *CE, const FunctionDecl *FD,
FName == "CMBufferQueueDequeueIfDataReadyAndRetain") {
// Part of: <rdar://problem/39390714>.
// These are not retain. They just return something and retain it.
return None;
return std::nullopt;
}
if (CE->getNumArgs() == 1 &&
(cocoa::isRefType(ResultTy, "CF", FName) ||
@ -781,7 +781,7 @@ RetainSummaryManager::canEval(const CallExpr *CE, const FunctionDecl *FD,
return BehaviorSummary::NoOp;
}
return None;
return std::nullopt;
}
const RetainSummary *
@ -885,7 +885,7 @@ RetainSummaryManager::getRetEffectFromAnnotations(QualType RetTy,
if (auto RE = getRetEffectFromAnnotations(RetTy, PD))
return RE;
return None;
return std::nullopt;
}
/// \return Whether the chain of typedefs starting from @c QT

View File

@ -90,7 +90,7 @@ void DeclToIndex::computeMap(const DeclContext &dc) {
Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
if (I == map.end())
return None;
return std::nullopt;
return I->second;
}