Skip to content

Commit

Permalink
[clangd] Don't run the prepare for tweaks that are disabled.
Browse files Browse the repository at this point in the history
Summary: Previously, we ran the prepare, even for the tweaks that are disabled.

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64565

llvm-svn: 365882
  • Loading branch information
hokein committed Jul 12, 2019
1 parent fafec51 commit 7eeb82b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
8 changes: 3 additions & 5 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
: nullptr),
GetClangTidyOptions(Opts.GetClangTidyOptions),
SuggestMissingIncludes(Opts.SuggestMissingIncludes),
EnableHiddenFeatures(Opts.HiddenFeatures), TweakFilter(Opts.TweakFilter),
TweakFilter(Opts.TweakFilter),
WorkspaceRoot(Opts.WorkspaceRoot),
// Pass a callback into `WorkScheduler` to extract symbols from a newly
// parsed file and rebuild the file index synchronously each time an AST
Expand Down Expand Up @@ -333,11 +333,9 @@ void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
if (!Selection)
return CB(Selection.takeError());
std::vector<TweakRef> Res;
for (auto &T : prepareTweaks(*Selection)) {
if (!TweakFilter(T->id()) || (T->hidden() && !EnableHiddenFeatures))
continue;
for (auto &T : prepareTweaks(*Selection, TweakFilter))
Res.push_back({T->id(), T->title(), T->intent()});
}

CB(std::move(Res));
};

Expand Down
13 changes: 5 additions & 8 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,17 @@ class ClangdServer {

bool SuggestMissingIncludes = false;

/// Enable hidden features mostly useful to clangd developers.
/// e.g. tweaks to dump the AST.
bool HiddenFeatures = false;

/// Clangd will execute compiler drivers matching one of these globs to
/// fetch system include path.
std::vector<std::string> QueryDriverGlobs;

/// Enable semantic highlighting features.
bool SemanticHighlighting = false;

/// Returns true if the StringRef is a tweak that should be enabled
std::function<bool(llvm::StringRef)> TweakFilter =
[](llvm::StringRef TweakToSearch) { return true; };
/// Returns true if the tweak should be enabled.
std::function<bool(const Tweak &)> TweakFilter = [](const Tweak &T) {
return !T.hidden(); // only enable non-hidden tweaks.
};
};
// Sensible default options for use in tests.
// Features like indexing must be enabled if desired.
Expand Down Expand Up @@ -322,7 +319,7 @@ class ClangdServer {
bool SuggestMissingIncludes = false;
bool EnableHiddenFeatures = false;

std::function<bool(llvm::StringRef)> TweakFilter;
std::function<bool(const Tweak &)> TweakFilter;

// GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
llvm::StringMap<llvm::Optional<FuzzyFindRequest>>
Expand Down
6 changes: 4 additions & 2 deletions clang-tools-extra/clangd/refactor/Tweak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
}

std::vector<std::unique_ptr<Tweak>> prepareTweaks(const Tweak::Selection &S) {
std::vector<std::unique_ptr<Tweak>>
prepareTweaks(const Tweak::Selection &S,
llvm::function_ref<bool(const Tweak &)> Filter) {
validateRegistry();

std::vector<std::unique_ptr<Tweak>> Available;
for (const auto &E : TweakRegistry::entries()) {
std::unique_ptr<Tweak> T = E.instantiate();
if (!T->prepare(S))
if (!Filter(*T) || !T->prepare(S))
continue;
Available.push_back(std::move(T));
}
Expand Down
8 changes: 5 additions & 3 deletions clang-tools-extra/clangd/refactor/Tweak.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ class Tweak {
TweakRegistrationFor##Subclass(#Subclass, /*Description=*/""); \
const char *Subclass::id() const { return #Subclass; }

/// Calls prepare() on all tweaks, returning those that can run on the
/// selection.
std::vector<std::unique_ptr<Tweak>> prepareTweaks(const Tweak::Selection &S);
/// Calls prepare() on all tweaks that satisfy the filter, returning those that
/// can run on the selection.
std::vector<std::unique_ptr<Tweak>>
prepareTweaks(const Tweak::Selection &S,
llvm::function_ref<bool(const Tweak &)> Filter);

// Calls prepare() on the tweak with a given ID.
// If prepare() returns false, returns an error.
Expand Down
14 changes: 8 additions & 6 deletions clang-tools-extra/clangd/tool/ClangdMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ int main(int argc, char *argv[]) {
}
Opts.StaticIndex = StaticIdx.get();
Opts.AsyncThreadsCount = WorkerThreadsCount;
Opts.HiddenFeatures = HiddenFeatures;

clangd::CodeCompleteOptions CCOpts;
CCOpts.IncludeIneligibleResults = IncludeIneligibleResults;
Expand Down Expand Up @@ -531,11 +530,14 @@ int main(int argc, char *argv[]) {
}
Opts.SuggestMissingIncludes = SuggestMissingIncludes;
Opts.QueryDriverGlobs = std::move(QueryDriverGlobs);
if (TweakList.getNumOccurrences())
Opts.TweakFilter = [&](llvm::StringRef TweakToSearch) {
// return true if any tweak matches the TweakToSearch
return llvm::find(TweakList, TweakToSearch) != TweakList.end();
};

Opts.TweakFilter = [&](const Tweak &T) {
if (T.hidden() && !HiddenFeatures)
return false;
if (TweakList.getNumOccurrences())
return llvm::is_contained(TweakList, T.id());
return true;
};
llvm::Optional<OffsetEncoding> OffsetEncodingFromFlag;
if (ForceOffsetEncoding != OffsetEncoding::UnsupportedEncoding)
OffsetEncodingFromFlag = ForceOffsetEncoding;
Expand Down

0 comments on commit 7eeb82b

Please sign in to comment.