Skip to content

Commit 059a23c

Browse files
[clang-tools-extra] 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
1 parent cd8702e commit 059a23c

Some content is hidden

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

60 files changed

+234
-231
lines changed

Diff for: clang-tools-extra/clang-doc/HTMLGenerator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ static std::unique_ptr<TagNode> genLink(const Twine &Text, const Twine &Link) {
308308

309309
static std::unique_ptr<HTMLNode>
310310
genReference(const Reference &Type, StringRef CurrentDirectory,
311-
llvm::Optional<StringRef> JumpToSection = None) {
311+
llvm::Optional<StringRef> JumpToSection = std::nullopt) {
312312
if (Type.Path.empty()) {
313313
if (!JumpToSection)
314314
return std::make_unique<TextNode>(Type.Name);
@@ -437,7 +437,7 @@ genReferencesBlock(const std::vector<Reference> &References,
437437

438438
static std::unique_ptr<TagNode>
439439
writeFileDefinition(const Location &L,
440-
llvm::Optional<StringRef> RepositoryUrl = None) {
440+
llvm::Optional<StringRef> RepositoryUrl = std::nullopt) {
441441
if (!L.IsFileInRootDir || !RepositoryUrl)
442442
return std::make_unique<TagNode>(
443443
HTMLTag::TAG_P, "Defined at line " + std::to_string(L.LineNumber) +

Diff for: clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
2626
std::string FilePath =
2727
getIncludePath(*SM, info->getDefinitionLoc(), Collector);
2828
if (FilePath.empty())
29-
return llvm::None;
29+
return std::nullopt;
3030
return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
3131
SymbolInfo::SymbolKind::Macro, FilePath, {});
3232
}

Diff for: clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ CreateSymbolInfo(const NamedDecl *ND, const SourceManager &SM,
8585
Type = SymbolInfo::SymbolKind::EnumDecl;
8686
// Ignore anonymous enum declarations.
8787
if (ND->getName().empty())
88-
return llvm::None;
88+
return std::nullopt;
8989
} else {
9090
assert(llvm::isa<RecordDecl>(ND) &&
9191
"Matched decl must be one of VarDecl, "
9292
"FunctionDecl, TypedefNameDecl, EnumConstantDecl, "
9393
"EnumDecl and RecordDecl!");
9494
// C-style record decl can have empty name, e.g "struct { ... } var;".
9595
if (ND->getName().empty())
96-
return llvm::None;
96+
return std::nullopt;
9797
Type = SymbolInfo::SymbolKind::Class;
9898
}
9999

@@ -102,11 +102,12 @@ CreateSymbolInfo(const NamedDecl *ND, const SourceManager &SM,
102102
llvm::errs() << "Declaration " << ND->getDeclName() << "("
103103
<< ND->getDeclKindName()
104104
<< ") has invalid declaration location.";
105-
return llvm::None;
105+
return std::nullopt;
106106
}
107107

108108
std::string FilePath = getIncludePath(SM, Loc, Collector);
109-
if (FilePath.empty()) return llvm::None;
109+
if (FilePath.empty())
110+
return std::nullopt;
110111

111112
return SymbolInfo(ND->getNameAsString(), Type, FilePath, GetContexts(ND));
112113
}

Diff for: clang-tools-extra/clang-query/Query.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
121121
continue;
122122

123123
TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note,
124-
"source locations here", None, None);
124+
"source locations here", std::nullopt, std::nullopt);
125125

126126
Iter = PrintLocations(OS, Iter, Locs.LocationAccessors.end());
127127
OS << '\n';
@@ -137,10 +137,10 @@ void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
137137
SM.getPresumedLineNumber(Iter->first.getEnd()))
138138
continue;
139139

140-
TD.emitDiagnostic(FullSourceLoc(Iter->first.getBegin(), SM),
141-
DiagnosticsEngine::Note,
142-
"source ranges here " + Iter->first.printToString(SM),
143-
CharSourceRange::getTokenRange(Iter->first), None);
140+
TD.emitDiagnostic(
141+
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
142+
"source ranges here " + Iter->first.printToString(SM),
143+
CharSourceRange::getTokenRange(Iter->first), std::nullopt);
144144

145145
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
146146
}
@@ -157,7 +157,7 @@ void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
157157
TD.emitDiagnostic(
158158
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
159159
"source range " + Iter->first.printToString(SM) + " starting here...",
160-
CharSourceRange::getTokenRange(Iter->first), None);
160+
CharSourceRange::getTokenRange(Iter->first), std::nullopt);
161161

162162
auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd());
163163
auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1));
@@ -166,7 +166,7 @@ void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
166166
DiagnosticsEngine::Note, "... ending here",
167167
CharSourceRange::getTokenRange(
168168
SourceRange(LastLineLoc, Iter->first.getEnd())),
169-
None);
169+
std::nullopt);
170170

171171
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
172172
}
@@ -232,7 +232,7 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
232232
TD.emitDiagnostic(
233233
FullSourceLoc(R.getBegin(), AST->getSourceManager()),
234234
DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here",
235-
CharSourceRange::getTokenRange(R), None);
235+
CharSourceRange::getTokenRange(R), std::nullopt);
236236
}
237237
}
238238
if (QS.PrintOutput) {

Diff for: clang-tools-extra/clangd/AST.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ getTemplateSpecializationArgLocs(const NamedDecl &ND) {
6565
}
6666
// We return None for ClassTemplateSpecializationDecls because it does not
6767
// contain TemplateArgumentLoc information.
68-
return llvm::None;
68+
return std::nullopt;
6969
}
7070

7171
template <class T>
@@ -571,7 +571,7 @@ llvm::Optional<QualType> getDeducedType(ASTContext &ASTCtx,
571571
DeducedTypeVisitor V(Loc);
572572
V.TraverseAST(ASTCtx);
573573
if (V.DeducedType.isNull())
574-
return llvm::None;
574+
return std::nullopt;
575575
return V.DeducedType;
576576
}
577577

@@ -862,7 +862,7 @@ class ForwardingCallVisitor
862862
return std::distance(Args.begin(), Begin);
863863
}
864864
}
865-
return llvm::None;
865+
return std::nullopt;
866866
}
867867

868868
static FunctionDecl *getCalleeDeclOrUniqueOverload(CallExpr *E) {

Diff for: clang-tools-extra/clangd/ClangdLSPServer.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ llvm::Optional<int64_t> decodeVersion(llvm::StringRef Encoded) {
6767
return Result;
6868
if (!Encoded.empty()) // Empty can be e.g. diagnostics on close.
6969
elog("unexpected non-numeric version {0}", Encoded);
70-
return llvm::None;
70+
return std::nullopt;
7171
}
7272

7373
const llvm::StringLiteral ApplyFixCommand = "clangd.applyFix";
@@ -808,7 +808,7 @@ void ClangdLSPServer::onWorkspaceSymbol(
808808
void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
809809
Callback<llvm::Optional<Range>> Reply) {
810810
Server->prepareRename(
811-
Params.textDocument.uri.file(), Params.position, /*NewName*/ llvm::None,
811+
Params.textDocument.uri.file(), Params.position, /*NewName*/ std::nullopt,
812812
Opts.Rename,
813813
[Reply = std::move(Reply)](llvm::Expected<RenameResult> Result) mutable {
814814
if (!Result)
@@ -890,7 +890,7 @@ void ClangdLSPServer::onDocumentFormatting(
890890
auto File = Params.textDocument.uri.file();
891891
auto Code = Server->getDraft(File);
892892
Server->formatFile(File,
893-
/*Rng=*/llvm::None,
893+
/*Rng=*/std::nullopt,
894894
[Code = std::move(Code), Reply = std::move(Reply)](
895895
llvm::Expected<tooling::Replacements> Result) mutable {
896896
if (Result)
@@ -951,14 +951,14 @@ void ClangdLSPServer::onFoldingRange(
951951
static llvm::Optional<Command> asCommand(const CodeAction &Action) {
952952
Command Cmd;
953953
if (Action.command && Action.edit)
954-
return None; // Not representable. (We never emit these anyway).
954+
return std::nullopt; // Not representable. (We never emit these anyway).
955955
if (Action.command) {
956956
Cmd = *Action.command;
957957
} else if (Action.edit) {
958958
Cmd.command = std::string(ApplyFixCommand);
959959
Cmd.argument = *Action.edit;
960960
} else {
961-
return None;
961+
return std::nullopt;
962962
}
963963
Cmd.title = Action.title;
964964
if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND)
@@ -1153,7 +1153,7 @@ void ClangdLSPServer::onSwitchSourceHeader(
11531153
return Reply(Path.takeError());
11541154
if (*Path)
11551155
return Reply(URIForFile::canonicalize(**Path, Params.uri.file()));
1156-
return Reply(llvm::None);
1156+
return Reply(std::nullopt);
11571157
});
11581158
}
11591159

@@ -1172,7 +1172,7 @@ void ClangdLSPServer::onHover(const TextDocumentPositionParams &Params,
11721172
if (!H)
11731173
return Reply(H.takeError());
11741174
if (!*H)
1175-
return Reply(llvm::None);
1175+
return Reply(std::nullopt);
11761176

11771177
Hover R;
11781178
R.contents.kind = HoverContentFormat;

Diff for: clang-tools-extra/clangd/ClangdServer.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class DraftStoreFS : public ThreadsafeFS {
140140
private:
141141
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override {
142142
auto OFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
143-
Base.view(llvm::None));
143+
Base.view(std::nullopt));
144144
OFS->pushOverlay(DirtyFiles.asVFS());
145145
return OFS;
146146
}
@@ -515,7 +515,7 @@ void ClangdServer::formatOnType(PathRef File, Position Pos,
515515
CB = std::move(CB), this]() mutable {
516516
auto Style = format::getStyle(format::DefaultFormatStyle, File,
517517
format::DefaultFallbackStyle, Code,
518-
TFS.view(/*CWD=*/llvm::None).get());
518+
TFS.view(/*CWD=*/std::nullopt).get());
519519
if (!Style)
520520
return CB(Style.takeError());
521521

@@ -566,7 +566,7 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
566566
if (!InpAST)
567567
return CB(InpAST.takeError());
568568
auto R = clangd::rename({Pos, NewName, InpAST->AST, File,
569-
DirtyFS->view(llvm::None), Index, Opts});
569+
DirtyFS->view(std::nullopt), Index, Opts});
570570
if (!R)
571571
return CB(R.takeError());
572572

@@ -659,7 +659,7 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
659659
this](Expected<InputsAndAST> InpAST) mutable {
660660
if (!InpAST)
661661
return CB(InpAST.takeError());
662-
auto FS = DirtyFS->view(llvm::None);
662+
auto FS = DirtyFS->view(std::nullopt);
663663
auto Selections = tweakSelection(Sel, *InpAST, FS.get());
664664
if (!Selections)
665665
return CB(Selections.takeError());
@@ -713,7 +713,7 @@ void ClangdServer::switchSourceHeader(
713713
// 2) if 1) fails, we use the AST&Index approach, it is slower but supports
714714
// different code layout.
715715
if (auto CorrespondingFile =
716-
getCorrespondingHeaderOrSource(Path, TFS.view(llvm::None)))
716+
getCorrespondingHeaderOrSource(Path, TFS.view(std::nullopt)))
717717
return CB(std::move(CorrespondingFile));
718718
auto Action = [Path = Path.str(), CB = std::move(CB),
719719
this](llvm::Expected<InputsAndAST> InpAST) mutable {
@@ -989,7 +989,7 @@ void ClangdServer::getAST(PathRef File, llvm::Optional<Range> R,
989989
return false;
990990
});
991991
if (!Success)
992-
CB(llvm::None);
992+
CB(std::nullopt);
993993
};
994994
WorkScheduler->runWithAST("GetAST", File, std::move(Action));
995995
}

Diff for: clang-tools-extra/clangd/ClangdServer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class ClangdServer {
142142
/// defaults and -resource-dir compiler flag).
143143
/// If None, ClangdServer calls CompilerInvocation::GetResourcePath() to
144144
/// obtain the standard resource directory.
145-
llvm::Optional<std::string> ResourceDir = llvm::None;
145+
llvm::Optional<std::string> ResourceDir = std::nullopt;
146146

147147
/// Time to wait after a new file version before computing diagnostics.
148148
DebouncePolicy UpdateDebounce = DebouncePolicy{

Diff for: clang-tools-extra/clangd/CodeComplete.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,14 @@ struct CompletionCandidate {
258258
headerToInsertIfAllowed(const CodeCompleteOptions &Opts) const {
259259
if (Opts.InsertIncludes == CodeCompleteOptions::NeverInsert ||
260260
RankedIncludeHeaders.empty())
261-
return None;
261+
return std::nullopt;
262262
if (SemaResult && SemaResult->Declaration) {
263263
// Avoid inserting new #include if the declaration is found in the current
264264
// file e.g. the symbol is forward declared.
265265
auto &SM = SemaResult->Declaration->getASTContext().getSourceManager();
266266
for (const Decl *RD : SemaResult->Declaration->redecls())
267267
if (SM.isInMainFile(SM.getExpansionLoc(RD->getBeginLoc())))
268-
return None;
268+
return std::nullopt;
269269
}
270270
return RankedIncludeHeaders[0];
271271
}
@@ -1821,7 +1821,7 @@ class CodeCompleteFlow {
18211821
(C.IndexResult &&
18221822
C.IndexResult->SymInfo.Kind == index::SymbolKind::Macro)) &&
18231823
!C.Name.startswith_insensitive(Filter->pattern()))
1824-
return None;
1824+
return std::nullopt;
18251825
return Filter->match(C.Name);
18261826
}
18271827

@@ -2049,7 +2049,7 @@ maybeFunctionArgumentCommentStart(llvm::StringRef Content) {
20492049
Content = Content.rtrim();
20502050
if (Content.endswith("/*"))
20512051
return Content.size() - 2;
2052-
return None;
2052+
return std::nullopt;
20532053
}
20542054

20552055
CodeCompleteResult codeComplete(PathRef FileName, Position Pos,

Diff for: clang-tools-extra/clangd/CompileCommands.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ llvm::Optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
4242
auto Xcrun = llvm::sys::findProgramByName("xcrun");
4343
if (!Xcrun) {
4444
log("Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
45-
return llvm::None;
45+
return std::nullopt;
4646
}
4747
llvm::SmallString<64> OutFile;
4848
llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile);
@@ -58,18 +58,18 @@ llvm::Optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
5858
"If you have a non-apple toolchain, this is OK. "
5959
"Otherwise, try xcode-select --install.",
6060
Ret);
61-
return llvm::None;
61+
return std::nullopt;
6262
}
6363

6464
auto Buf = llvm::MemoryBuffer::getFile(OutFile);
6565
if (!Buf) {
6666
log("Can't read xcrun output: {0}", Buf.getError().message());
67-
return llvm::None;
67+
return std::nullopt;
6868
}
6969
StringRef Path = Buf->get()->getBuffer().trim();
7070
if (Path.empty()) {
7171
log("xcrun produced no output");
72-
return llvm::None;
72+
return std::nullopt;
7373
}
7474
return Path.str();
7575
}
@@ -120,12 +120,12 @@ std::string detectClangPath() {
120120
// The effect of this is to set -isysroot correctly. We do the same.
121121
llvm::Optional<std::string> detectSysroot() {
122122
#ifndef __APPLE__
123-
return llvm::None;
123+
return std::nullopt;
124124
#endif
125125

126126
// SDKROOT overridden in environment, respect it. Driver will set isysroot.
127127
if (::getenv("SDKROOT"))
128-
return llvm::None;
128+
return std::nullopt;
129129
return queryXcrun({"xcrun", "--show-sdk-path"});
130130
}
131131

Diff for: clang-tools-extra/clangd/Config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct Config {
6565
std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
6666
Edits;
6767
/// Where to search for compilation databases for this file's flags.
68-
CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, llvm::None};
68+
CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, std::nullopt};
6969
} CompileFlags;
7070

7171
enum class BackgroundPolicy { Build, Skip };

Diff for: clang-tools-extra/clangd/ConfigCompile.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct FragmentCompiler {
111111
std::string RegexError;
112112
if (!Result.isValid(RegexError)) {
113113
diag(Error, "Invalid regex " + Anchored + ": " + RegexError, Text.Range);
114-
return llvm::None;
114+
return std::nullopt;
115115
}
116116
return Result;
117117
}
@@ -129,7 +129,7 @@ struct FragmentCompiler {
129129
Description)
130130
.str(),
131131
Path.Range);
132-
return llvm::None;
132+
return std::nullopt;
133133
}
134134
llvm::SmallString<256> AbsPath = llvm::StringRef(*Path);
135135
llvm::sys::fs::make_absolute(FragmentDirectory, AbsPath);

0 commit comments

Comments
 (0)