[clang-tools-extra] 7dc388b - [clangd] Make Tweak::Selection movable. NFC
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 16 06:47:07 PST 2019
Author: Sam McCall
Date: 2019-12-16T15:46:57+01:00
New Revision: 7dc388bd9596bbf42633f8a8e450224e39740b60
URL: https://github.com/llvm/llvm-project/commit/7dc388bd9596bbf42633f8a8e450224e39740b60
DIFF: https://github.com/llvm/llvm-project/commit/7dc388bd9596bbf42633f8a8e450224e39740b60.diff
LOG: [clangd] Make Tweak::Selection movable. NFC
Added:
Modified:
clang-tools-extra/clangd/refactor/Tweak.cpp
clang-tools-extra/clangd/refactor/Tweak.h
clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/refactor/Tweak.cpp b/clang-tools-extra/clangd/refactor/Tweak.cpp
index 2dc091ed762a..435c36e91efa 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.cpp
+++ b/clang-tools-extra/clangd/refactor/Tweak.cpp
@@ -47,7 +47,7 @@ void validateRegistry() {
Tweak::Selection::Selection(const SymbolIndex *Index, ParsedAST &AST,
unsigned RangeBegin, unsigned RangeEnd)
- : Index(Index), AST(AST), SelectionBegin(RangeBegin),
+ : Index(Index), AST(&AST), SelectionBegin(RangeBegin),
SelectionEnd(RangeEnd),
ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) {
auto &SM = AST.getSourceManager();
diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h
index 69ac4ad612e9..14f9ffca4437 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.h
+++ b/clang-tools-extra/clangd/refactor/Tweak.h
@@ -53,8 +53,8 @@ class Tweak {
llvm::StringRef Code;
/// The Index for handling codebase related queries.
const SymbolIndex *Index = nullptr;
- /// Parsed AST of the active file.
- ParsedAST *
+ /// The parsed active file. Never null. (Pointer so Selection is movable).
+ ParsedAST *AST;
/// A location of the cursor in the editor.
// FIXME: Cursor is redundant and should be removed
SourceLocation Cursor;
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
index 2d4d2ac24ea6..8e3eba35b004 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -44,18 +44,18 @@ Expected<Tweak::Effect> AnnotateHighlightings::apply(const Selection &Inputs) {
// Now we hit the TUDecl case where commonAncestor() returns null
// intendedly. We only annotate tokens in the main file, so use the default
// traversal scope (which is the top level decls of the main file).
- HighlightingTokens = getSemanticHighlightings(Inputs.AST);
+ HighlightingTokens = getSemanticHighlightings(*Inputs.AST);
} else {
// Store the existing scopes.
- const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
+ const auto &BackupScopes = Inputs.AST->getASTContext().getTraversalScope();
// Narrow the traversal scope to the selected node.
- Inputs.AST.getASTContext().setTraversalScope(
+ Inputs.AST->getASTContext().setTraversalScope(
{const_cast<Decl *>(CommonDecl)});
- HighlightingTokens = getSemanticHighlightings(Inputs.AST);
+ HighlightingTokens = getSemanticHighlightings(*Inputs.AST);
// Restore the traversal scope.
- Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
+ Inputs.AST->getASTContext().setTraversalScope(BackupScopes);
}
- auto &SM = Inputs.AST.getSourceManager();
+ auto &SM = Inputs.AST->getSourceManager();
tooling::Replacements Result;
llvm::StringRef FilePath = SM.getFilename(Inputs.Cursor);
for (const auto &Token : HighlightingTokens) {
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
index 57690ee3d684..9db7e302ce6c 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineInline.cpp
@@ -438,15 +438,15 @@ class DefineInline : public Tweak {
// Check if the decls referenced in function body are visible in the
// declaration location.
- if (!checkDeclsAreVisible(getNonLocalDeclRefs(Sel.AST, Source), Target,
- Sel.AST.getSourceManager()))
+ if (!checkDeclsAreVisible(getNonLocalDeclRefs(*Sel.AST, Source), Target,
+ Sel.AST->getSourceManager()))
return false;
return true;
}
Expected<Effect> apply(const Selection &Sel) override {
- const auto &AST = Sel.AST.getASTContext();
+ const auto &AST = Sel.AST->getASTContext();
const auto &SM = AST.getSourceManager();
auto Semicolon = getSemicolonForDecl(Target);
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
index 353da26a0f91..6d27149ef6d4 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
@@ -65,9 +65,9 @@ llvm::Optional<Path> getSourceFile(llvm::StringRef FileName,
const Tweak::Selection &Sel) {
if (auto Source = getCorrespondingHeaderOrSource(
FileName,
- &Sel.AST.getSourceManager().getFileManager().getVirtualFileSystem()))
+ &Sel.AST->getSourceManager().getFileManager().getVirtualFileSystem()))
return *Source;
- return getCorrespondingHeaderOrSource(FileName, Sel.AST, Sel.Index);
+ return getCorrespondingHeaderOrSource(FileName, *Sel.AST, Sel.Index);
}
// Synthesize a DeclContext for TargetNS from CurContext. TargetNS must be empty
@@ -309,8 +309,8 @@ class DefineOutline : public Tweak {
// Bail out if we are not in a header file.
// FIXME: We might want to consider moving method definitions below class
// definition even if we are inside a source file.
- if (!isHeaderFile(Sel.AST.getSourceManager().getFilename(Sel.Cursor),
- Sel.AST.getLangOpts()))
+ if (!isHeaderFile(Sel.AST->getSourceManager().getFilename(Sel.Cursor),
+ Sel.AST->getLangOpts()))
return false;
Source = getSelectedFunction(Sel.ASTSelection.commonAncestor());
@@ -333,7 +333,7 @@ class DefineOutline : public Tweak {
}
Expected<Effect> apply(const Selection &Sel) override {
- const SourceManager &SM = Sel.AST.getSourceManager();
+ const SourceManager &SM = Sel.AST->getSourceManager();
auto MainFileName =
getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
if (!MainFileName)
@@ -348,7 +348,7 @@ class DefineOutline : public Tweak {
"Couldn't find a suitable implementation file.");
auto &FS =
- Sel.AST.getSourceManager().getFileManager().getVirtualFileSystem();
+ Sel.AST->getSourceManager().getFileManager().getVirtualFileSystem();
auto Buffer = FS.getBufferForFile(*CCFile);
// FIXME: Maybe we should consider creating the implementation file if it
// doesn't exist?
@@ -363,7 +363,7 @@ class DefineOutline : public Tweak {
return InsertionPoint.takeError();
auto FuncDef = getFunctionSourceCode(
- Source, InsertionPoint->EnclosingNamespace, Sel.AST.getTokens());
+ Source, InsertionPoint->EnclosingNamespace, Sel.AST->getTokens());
if (!FuncDef)
return FuncDef.takeError();
@@ -377,10 +377,10 @@ class DefineOutline : public Tweak {
// FIXME: We should also get rid of inline qualifier.
const tooling::Replacement DeleteFuncBody(
- Sel.AST.getSourceManager(),
+ Sel.AST->getSourceManager(),
CharSourceRange::getTokenRange(*toHalfOpenFileRange(
- SM, Sel.AST.getLangOpts(),
- getDeletionRange(Source, Sel.AST.getTokens()))),
+ SM, Sel.AST->getLangOpts(),
+ getDeletionRange(Source, Sel.AST->getTokens()))),
";");
auto HeaderFE = Effect::fileEdit(SM, SM.getMainFileID(),
tooling::Replacements(DeleteFuncBody));
diff --git a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
index 879f4d23c24b..f6814003b1ce 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/DumpAST.cpp
@@ -61,7 +61,7 @@ REGISTER_TWEAK(DumpAST)
llvm::Expected<Tweak::Effect> DumpAST::apply(const Selection &Inputs) {
std::string Str;
llvm::raw_string_ostream OS(Str);
- Node->dump(OS, Inputs.AST.getSourceManager());
+ Node->dump(OS, Inputs.AST->getSourceManager());
return Effect::showMessage(std::move(OS.str()));
}
@@ -110,8 +110,8 @@ class DumpSymbol : public Tweak {
llvm::raw_string_ostream Out(Storage);
for (auto &Sym : getSymbolInfo(
- Inputs.AST,
- sourceLocToPosition(Inputs.AST.getSourceManager(), Inputs.Cursor)))
+ *Inputs.AST, sourceLocToPosition(Inputs.AST->getSourceManager(),
+ Inputs.Cursor)))
Out << Sym;
return Effect::showMessage(Out.str());
}
@@ -144,7 +144,7 @@ class DumpRecordLayout : public Tweak {
Expected<Effect> apply(const Selection &Inputs) override {
std::string Str;
llvm::raw_string_ostream OS(Str);
- Inputs.AST.getASTContext().DumpRecordLayout(Record, OS);
+ Inputs.AST->getASTContext().DumpRecordLayout(Record, OS);
return Effect::showMessage(std::move(OS.str()));
}
std::string title() const override {
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index a17324d07dbb..bffd6c02fb6e 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -71,10 +71,10 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
}
Expected<Tweak::Effect> ExpandAutoType::apply(const Selection& Inputs) {
- auto& SrcMgr = Inputs.AST.getSourceManager();
+ auto &SrcMgr = Inputs.AST->getSourceManager();
- llvm::Optional<clang::QualType> DeducedType =
- getDeducedType(Inputs.AST.getASTContext(), CachedLocation->getBeginLoc());
+ llvm::Optional<clang::QualType> DeducedType = getDeducedType(
+ Inputs.AST->getASTContext(), CachedLocation->getBeginLoc());
// if we can't resolve the type, return an error message
if (DeducedType == llvm::None)
@@ -107,7 +107,7 @@ Expected<Tweak::Effect> ExpandAutoType::apply(const Selection& Inputs) {
llvm::Error ExpandAutoType::createErrorMessage(const std::string& Message,
const Selection& Inputs) {
- auto& SrcMgr = Inputs.AST.getSourceManager();
+ auto &SrcMgr = Inputs.AST->getSourceManager();
std::string ErrorMessage =
Message + ": " +
SrcMgr.getFilename(Inputs.Cursor).str() + " Line " +
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
index 89025270b649..eb15d03b32d7 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandMacro.cpp
@@ -90,21 +90,21 @@ bool ExpandMacro::prepare(const Selection &Inputs) {
// 'FOO[[ ]]BAR'. We should not trigger in that case.
// Find a token under the cursor.
- auto *T = findIdentifierUnderCursor(Inputs.AST.getTokens(), Inputs.Cursor);
+ auto *T = findIdentifierUnderCursor(Inputs.AST->getTokens(), Inputs.Cursor);
// We are interested only in identifiers, other tokens can't be macro names.
if (!T)
return false;
// If the identifier is a macro we will find the corresponding expansion.
- auto Expansion = Inputs.AST.getTokens().expansionStartingAt(T);
+ auto Expansion = Inputs.AST->getTokens().expansionStartingAt(T);
if (!Expansion)
return false;
- this->MacroName = T->text(Inputs.AST.getSourceManager());
+ this->MacroName = T->text(Inputs.AST->getSourceManager());
this->Expansion = *Expansion;
return true;
}
Expected<Tweak::Effect> ExpandMacro::apply(const Selection &Inputs) {
- auto &SM = Inputs.AST.getSourceManager();
+ auto &SM = Inputs.AST->getSourceManager();
std::string Replacement;
for (const syntax::Token &T : Expansion.Expanded) {
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
index b99599c7f8f6..da8e6e07be68 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
@@ -681,8 +681,8 @@ tooling::Replacement createFunctionDefinition(const NewFunction &ExtractedFunc,
bool ExtractFunction::prepare(const Selection &Inputs) {
const Node *CommonAnc = Inputs.ASTSelection.commonAncestor();
- const SourceManager &SM = Inputs.AST.getSourceManager();
- const LangOptions &LangOpts = Inputs.AST.getLangOpts();
+ const SourceManager &SM = Inputs.AST->getSourceManager();
+ const LangOptions &LangOpts = Inputs.AST->getLangOpts();
if (auto MaybeExtZone = findExtractionZone(CommonAnc, SM, LangOpts)) {
ExtZone = std::move(*MaybeExtZone);
return true;
@@ -691,8 +691,8 @@ bool ExtractFunction::prepare(const Selection &Inputs) {
}
Expected<Tweak::Effect> ExtractFunction::apply(const Selection &Inputs) {
- const SourceManager &SM = Inputs.AST.getSourceManager();
- const LangOptions &LangOpts = Inputs.AST.getLangOpts();
+ const SourceManager &SM = Inputs.AST->getSourceManager();
+ const LangOptions &LangOpts = Inputs.AST->getLangOpts();
auto ExtractedFunc = getExtractedFunction(ExtZone, SM, LangOpts);
// FIXME: Add more types of errors.
if (!ExtractedFunc)
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
index b758e69378fa..83bc901a3f2f 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp
@@ -449,8 +449,8 @@ bool ExtractVariable::prepare(const Selection &Inputs) {
// we don't trigger on empty selections for now
if (Inputs.SelectionBegin == Inputs.SelectionEnd)
return false;
- const ASTContext &Ctx = Inputs.AST.getASTContext();
- const SourceManager &SM = Inputs.AST.getSourceManager();
+ const ASTContext &Ctx = Inputs.AST->getASTContext();
+ const SourceManager &SM = Inputs.AST->getSourceManager();
if (const SelectionTree::Node *N =
computeExtractedExpr(Inputs.ASTSelection.commonAncestor()))
Target = std::make_unique<ExtractionContext>(N, SM, Ctx);
@@ -468,7 +468,8 @@ Expected<Tweak::Effect> ExtractVariable::apply(const Selection &Inputs) {
// replace expression with variable name
if (auto Err = Result.add(Target->replaceWithVar(Range, VarName)))
return std::move(Err);
- return Effect::mainFileEdit(Inputs.AST.getSourceManager(), std::move(Result));
+ return Effect::mainFileEdit(Inputs.AST->getSourceManager(),
+ std::move(Result));
}
} // namespace
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
index 62d0c6a2d20c..4e2521911072 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ObjCLocalizeStringLiteral.cpp
@@ -63,13 +63,13 @@ bool ObjCLocalizeStringLiteral::prepare(const Selection &Inputs) {
Expected<Tweak::Effect>
ObjCLocalizeStringLiteral::apply(const Selection &Inputs) {
- auto &SM = Inputs.AST.getSourceManager();
- auto &LangOpts = Inputs.AST.getASTContext().getLangOpts();
+ auto &SM = Inputs.AST->getSourceManager();
+ auto &LangOpts = Inputs.AST->getASTContext().getLangOpts();
auto Reps = tooling::Replacements(tooling::Replacement(
SM, CharSourceRange::getCharRange(Str->getBeginLoc()),
"NSLocalizedString(", LangOpts));
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
- Str->getEndLoc(), 0, Inputs.AST.getSourceManager(), LangOpts);
+ Str->getEndLoc(), 0, Inputs.AST->getSourceManager(), LangOpts);
if (auto Err = Reps.add(tooling::Replacement(
SM, CharSourceRange::getCharRange(EndLoc), ", @\"\")", LangOpts)))
return std::move(Err);
diff --git a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
index 2d4bf755f64f..c5f480cacd76 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RawStringLiteral.cpp
@@ -83,15 +83,15 @@ bool RawStringLiteral::prepare(const Selection &Inputs) {
return false;
Str = dyn_cast_or_null<StringLiteral>(N->ASTNode.get<Stmt>());
return Str &&
- isNormalString(*Str, Inputs.Cursor, Inputs.AST.getSourceManager()) &&
+ isNormalString(*Str, Inputs.Cursor, Inputs.AST->getSourceManager()) &&
needsRaw(Str->getBytes()) && canBeRaw(Str->getBytes());
}
Expected<Tweak::Effect> RawStringLiteral::apply(const Selection &Inputs) {
- auto &SM = Inputs.AST.getSourceManager();
+ auto &SM = Inputs.AST->getSourceManager();
auto Reps = tooling::Replacements(
tooling::Replacement(SM, Str, ("R\"(" + Str->getBytes() + ")\"").str(),
- Inputs.AST.getLangOpts()));
+ Inputs.AST->getLangOpts()));
return Effect::mainFileEdit(SM, std::move(Reps));
}
diff --git a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
index 65111254c44a..d2edde314941 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp
@@ -126,7 +126,7 @@ bool RemoveUsingNamespace::prepare(const Selection &Inputs) {
}
Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
- auto &Ctx = Inputs.AST.getASTContext();
+ auto &Ctx = Inputs.AST->getASTContext();
auto &SM = Ctx.getSourceManager();
// First, collect *all* using namespace directives that redeclare the same
// namespace.
@@ -143,7 +143,7 @@ Expected<Tweak::Effect> RemoveUsingNamespace::apply(const Selection &Inputs) {
// Collect all references to symbols from the namespace for which we're
// removing the directive.
std::vector<SourceLocation> IdentsToQualify;
- for (auto &D : Inputs.AST.getLocalTopLevelDecls()) {
+ for (auto &D : Inputs.AST->getLocalTopLevelDecls()) {
findExplicitReferences(D, [&](ReferenceLoc Ref) {
if (Ref.Qualifier)
return; // This reference is already qualified.
diff --git a/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp b/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
index a63b71b392c2..1d57cd5c7132 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/SwapIfBranches.cpp
@@ -62,8 +62,8 @@ bool SwapIfBranches::prepare(const Selection &Inputs) {
}
Expected<Tweak::Effect> SwapIfBranches::apply(const Selection &Inputs) {
- auto &Ctx = Inputs.AST.getASTContext();
- auto &SrcMgr = Inputs.AST.getSourceManager();
+ auto &Ctx = Inputs.AST->getASTContext();
+ auto &SrcMgr = Inputs.AST->getSourceManager();
auto ThenRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
If->getThen()->getSourceRange());
More information about the cfe-commits
mailing list