27#include "../clang-tidy/ClangTidyModule.h"
28#include "../clang-tidy/ClangTidyModuleRegistry.h"
29#include "../clang-tidy/ClangTidyOptions.h"
30#include "../clang-tidy/GlobList.h"
50#include "clang-include-cleaner/Record.h"
57#include "clang/AST/ASTContext.h"
58#include "clang/Basic/Diagnostic.h"
59#include "clang/Basic/LLVM.h"
60#include "clang/Format/Format.h"
61#include "clang/Frontend/CompilerInvocation.h"
62#include "clang/Tooling/CompilationDatabase.h"
63#include "llvm/ADT/ArrayRef.h"
64#include "llvm/ADT/STLExtras.h"
65#include "llvm/ADT/SmallString.h"
66#include "llvm/Support/Chrono.h"
67#include "llvm/Support/CommandLine.h"
68#include "llvm/Support/Path.h"
69#include "llvm/Support/Process.h"
84llvm::cl::opt<std::string> CheckTidyTime{
86 llvm::cl::desc(
"Print the overhead of checks matching this glob"),
88llvm::cl::opt<std::string> CheckFileLines{
91 "Limits the range of tokens in -check file on which "
92 "various features are tested. Example --check-lines=3-7 restricts "
93 "testing to lines 3 to 7 (inclusive) or --check-lines=5 to restrict "
94 "to one line. Default is testing entire file."),
96llvm::cl::opt<bool> CheckLocations{
99 "Runs certain features (e.g. hover) at each point in the file. "
101 llvm::cl::init(
true)};
102llvm::cl::opt<bool> CheckCompletion{
104 llvm::cl::desc(
"Run code-completion at each point (slow)"),
105 llvm::cl::init(
false)};
108unsigned showErrors(llvm::ArrayRef<Diag> Diags) {
110 for (
const auto &D : Diags) {
111 if (
D.Severity >= DiagnosticsEngine::Error) {
112 elog(
"[{0}] Line {1}: {2}",
D.Name,
D.Range.start.line + 1,
D.Message);
119std::vector<std::string> listTidyChecks(llvm::StringRef Glob) {
120 tidy::GlobList G(Glob);
121 tidy::ClangTidyCheckFactories CTFactories;
122 for (
const auto &
E : tidy::ClangTidyModuleRegistry::entries())
123 E.instantiate()->addCheckFactories(CTFactories);
124 std::vector<std::string> Result;
125 for (
const auto &
E : CTFactories)
126 if (G.contains(
E.getKey()))
127 Result.push_back(
E.getKey().str());
140 ClangdLSPServer::Options Opts;
142 tooling::CompileCommand Cmd;
145 std::unique_ptr<CompilerInvocation> Invocation;
146 format::FormatStyle Style;
148 std::shared_ptr<const PreambleData>
Preamble;
149 std::optional<ParsedAST> AST;
156 Checker(llvm::StringRef File,
const ClangdLSPServer::Options &Opts)
157 : File(File), Opts(Opts) {}
160 bool buildCommand(
const ThreadsafeFS &TFS) {
161 log(
"Loading compilation database...");
162 DirectoryBasedGlobalCompilationDatabase::Options CDBOpts(TFS);
163 CDBOpts.CompileCommandsDir =
165 std::unique_ptr<GlobalCompilationDatabase> BaseCDB =
166 std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts);
168 Mangler.SystemIncludeExtractor =
170 if (Opts.ResourceDir)
171 Mangler.ResourceDir = *Opts.ResourceDir;
172 auto CDB = std::make_unique<OverlayCDB>(
173 BaseCDB.get(), std::vector<std::string>{}, std::move(Mangler));
175 if (
auto TrueCmd = CDB->getCompileCommand(File)) {
176 Cmd = std::move(*TrueCmd);
177 log(
"Compile command {0} is: [{1}] {2}",
178 Cmd.Heuristic.empty() ?
"from CDB" : Cmd.Heuristic, Cmd.Directory,
181 Cmd = CDB->getFallbackCommand(File);
182 log(
"Generic fallback command is: [{0}] {1}", Cmd.Directory,
190 bool buildInvocation(
const ThreadsafeFS &TFS,
191 std::optional<std::string> Contents) {
192 StoreDiags CaptureInvocationDiags;
193 std::vector<std::string> CC1Args;
194 Inputs.CompileCommand = Cmd;
196 Inputs.ClangTidyProvider = Opts.ClangTidyProvider;
197 Inputs.Opts.PreambleParseForwardingFunctions =
198 Opts.PreambleParseForwardingFunctions;
200 Inputs.Contents = *Contents;
201 log(
"Imaginary source file contents:\n{0}", Inputs.Contents);
203 if (
auto Contents = TFS.view(std::nullopt)->getBufferForFile(File)) {
204 Inputs.Contents = Contents->get()->getBuffer().str();
206 elog(
"Couldn't read {0}: {1}", File, Contents.getError().message());
210 log(
"Parsing command...");
213 auto InvocationDiags = CaptureInvocationDiags.take();
214 ErrCount += showErrors(InvocationDiags);
215 log(
"internal (cc1) args are: {0}",
printArgv(CC1Args));
217 elog(
"Failed to parse command line");
230 log(
"Building preamble...");
232 File, *Invocation, Inputs,
true,
233 [&](CapturedASTCtx Ctx,
234 std::shared_ptr<const include_cleaner::PragmaIncludes> PI) {
235 if (!Opts.BuildDynamicSymbolIndex)
237 log(
"Indexing headers...");
238 Index.updatePreamble(File,
"null", Ctx.getASTContext(),
239 Ctx.getPreprocessor(), *PI);
242 elog(
"Failed to build preamble");
247 log(
"Building AST...");
251 elog(
"Failed to build AST");
255 showErrors(AST->getDiagnostics().drop_front(
Preamble->Diags.size()));
257 if (Opts.BuildDynamicSymbolIndex) {
258 log(
"Indexing AST...");
259 Index.updateMain(File, *AST);
262 if (!CheckTidyTime.empty()) {
263 if (!CLANGD_TIDY_CHECKS) {
264 elog(
"-{0} requires -DCLANGD_TIDY_CHECKS!", CheckTidyTime.ArgStr);
268 elog(
"Timing clang-tidy checks in asserts-mode is not representative!");
283 void checkTidyTimes() {
284 double Stability = 0.03;
285 log(
"Timing AST build with individual clang-tidy checks (target accuracy "
289 using Duration = std::chrono::nanoseconds;
291 auto Time = [&](
auto &&Run) -> Duration {
292 llvm::sys::TimePoint<> Elapsed;
293 std::chrono::nanoseconds UserBegin, UserEnd, System;
294 llvm::sys::Process::GetTimeUsage(Elapsed, UserBegin, System);
296 llvm::sys::Process::GetTimeUsage(Elapsed, UserEnd, System);
297 return UserEnd - UserBegin;
299 auto Change = [&](Duration Exp, Duration Base) ->
double {
300 return (
double)(Exp.count() - Base.count()) / Base.count();
303 auto Build = [&](llvm::StringRef Checks) -> Duration {
304 TidyProvider CTProvider = [&](tidy::ClangTidyOptions &Opts,
306 Opts.Checks = Checks.str();
308 Inputs.ClangTidyProvider = CTProvider;
312 Duration Val = Time([&] {
315 vlog(
" Measured {0} ==> {1}", Checks, Val);
319 auto MedianTime = [&](llvm::StringRef Checks) -> Duration {
320 std::array<Duration, 5> Measurements;
321 for (
auto &
M : Measurements)
323 llvm::sort(Measurements);
324 return Measurements[Measurements.size() / 2];
326 Duration Baseline = MedianTime(
"-*");
327 log(
" Baseline = {0}", Baseline);
329 auto Measure = [&](llvm::StringRef Check) ->
double {
331 Duration Median = MedianTime((
"-*," + Check).str());
332 Duration NewBase = MedianTime(
"-*");
335 double DeltaFraction = Change(NewBase, Baseline);
337 vlog(
" Baseline = {0}", Baseline);
338 if (DeltaFraction < -Stability || DeltaFraction > Stability) {
339 elog(
" Speed unstable, discarding measurement.");
342 return Change(Median, Baseline);
346 for (
const auto& Check : listTidyChecks(CheckTidyTime)) {
348 vlog(
" Timing {0}", Check);
349 double Fraction = Measure(Check);
350 log(
" {0} = {1:P0}", Check, Fraction);
352 log(
"Finished individual clang-tidy checks");
355 Inputs.ClangTidyProvider = Opts.ClangTidyProvider;
359 void buildInlayHints(std::optional<Range> LineRange) {
360 log(
"Building inlay hints");
363 for (
const auto &Hint :
Hints) {
364 vlog(
" {0} {1} {2}", Hint.kind, Hint.position, Hint.label);
368 void buildSemanticHighlighting(std::optional<Range> LineRange) {
369 log(
"Building semantic highlighting");
372 for (
const auto HL : Highlights)
373 if (!LineRange || LineRange->contains(HL.R))
374 vlog(
" {0} {1} {2}", HL.R, HL.Kind, HL.Modifiers);
378 void testLocationFeatures(std::optional<Range> LineRange) {
379 trace::Span Trace(
"testLocationFeatures");
380 log(
"Testing features at each token (may be slow in large files)");
381 auto &SM = AST->getSourceManager();
382 auto SpelledTokens = AST->getTokens().spelledTokens(SM.getMainFileID());
384 CodeCompleteOptions CCOpts = Opts.CodeComplete;
385 CCOpts.Index = &Index;
387 for (
const auto &Tok : SpelledTokens) {
388 unsigned Start = AST->getSourceManager().getFileOffset(Tok.location());
389 unsigned End = Start + Tok.length();
392 if (LineRange && !LineRange->contains(
Pos))
395 trace::Span Trace(
"Token");
397 SPAN_ATTACH(Trace,
"text", Tok.text(AST->getSourceManager()));
401 vlog(
" {0} {1}",
Pos, Tok.text(AST->getSourceManager()));
403 AST->getTokens(), Start, End);
404 Tweak::Selection Selection(&Index, *AST, Start, End, std::move(Tree),
409 prepareTweaks(Selection, Opts.TweakFilter, Opts.FeatureModules);
411 &AST->getSourceManager().getFileManager().getVirtualFileSystem();
412 for (
const auto &T : Tweaks) {
413 auto Result = T->apply(Selection);
415 elog(
" tweak: {0} ==> FAIL: {1}", T->id(), Result.takeError());
418 vlog(
" tweak: {0}", T->id());
422 vlog(
" definition: {0}", Definitions);
425 vlog(
" hover: {0}", Hover.has_value());
428 vlog(
" documentHighlight: {0}", DocHighlights);
430 if (CheckCompletion) {
433 vlog(
" code completion: {0}",
434 CC.Completions.empty() ?
"<empty>" :
CC.Completions[0].Name);
444 std::optional<Range> LineRange;
445 if (!CheckFileLines.empty()) {
446 uint32_t Begin = 0, End = std::numeric_limits<uint32_t>::max();
447 StringRef RangeStr(CheckFileLines);
448 bool ParseError = RangeStr.consumeInteger(0, Begin);
449 if (RangeStr.empty()) {
453 ParseError |= RangeStr.consumeInteger(0, End);
455 if (
ParseError || !RangeStr.empty() || Begin <= 0 || End < Begin) {
456 elog(
"Invalid --check-lines specified. Use Begin-End format, e.g. 3-17");
460 Position{
static_cast<int>(End), 0}};
463 llvm::SmallString<0> FakeFile;
464 std::optional<std::string> Contents;
466 llvm::sys::path::system_temp_directory(
false, FakeFile);
467 llvm::sys::path::append(FakeFile,
"test.cc");
474 auto xxx = std::string(N, 'x');
477 log("Testing on source file {0}",
File);
480 Opts.ConfigProvider,
nullptr);
485 Checker
C(
File, Opts);
486 if (!
C.buildCommand(TFS) || !
C.buildInvocation(TFS, Contents) ||
489 C.buildInlayHints(LineRange);
490 C.buildSemanticHighlighting(LineRange);
492 C.testLocationFeatures(LineRange);
494 log(
"All checks completed, {0} errors",
C.ErrCount);
495 return C.ErrCount == 0;
const PreambleData & Preamble
std::vector< FixItHint > Hints
const google::protobuf::Message & M
#define SPAN_ATTACH(S, Name, Expr)
Attach a key-value pair to a Span event.
static std::function< Context(PathRef)> createConfiguredContextProvider(const config::Provider *Provider, ClangdServer::Callbacks *)
Creates a context provider that loads and installs config.
static std::optional< ParsedAST > build(llvm::StringRef Filename, const ParseInputs &Inputs, std::unique_ptr< clang::CompilerInvocation > CI, llvm::ArrayRef< Diag > CompilerInvocationDiags, std::shared_ptr< const PreambleData > Preamble)
Attempts to run Clang and store the parsed AST.
static SelectionTree createRight(ASTContext &AST, const syntax::TokenBuffer &Tokens, unsigned Begin, unsigned End)
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
WithContext replaces Context::current() with a provided scope.
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens)
SystemIncludeExtractorFn getSystemIncludeExtractor(llvm::ArrayRef< std::string > QueryDriverGlobs)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
format::FormatStyle getFormatStyleForFile(llvm::StringRef File, llvm::StringRef Content, const ThreadsafeFS &TFS)
Choose the clang-format style we should apply to a certain file.
void vlog(const char *Fmt, Ts &&... Vals)
std::optional< HoverInfo > getHover(ParsedAST &AST, Position Pos, const format::FormatStyle &Style, const SymbolIndex *Index)
Get the hover information when hovering at Pos.
std::vector< LocatedSymbol > locateSymbolAt(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Get definition of symbol at a specified Pos.
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation CI, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback, PreambleBuildStats *Stats)
Build a preamble for the new inputs unless an old one can be reused.
void log(const char *Fmt, Ts &&... Vals)
std::string printArgv(llvm::ArrayRef< llvm::StringRef > Args)
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter, const FeatureModuleSet *Modules)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection.
std::vector< InlayHint > inlayHints(ParsedAST &AST, std::optional< Range > RestrictRange)
Compute and return inlay hints for a file.
llvm::unique_function< void(tidy::ClangTidyOptions &, llvm::StringRef) const > TidyProvider
A factory to modify a tidy::ClangTidyOptions.
CodeCompleteResult codeComplete(PathRef FileName, Position Pos, const PreambleData *Preamble, const ParseInputs &ParseInput, CodeCompleteOptions Opts, SpeculativeFuzzyFind *SpecFuzzyFind)
Gets code completions at a specified Pos in FileName.
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
void elog(const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static CommandMangler detect()
std::optional< std::string > FixedCDBPath
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
CDBSearchSpec CDBSearch
Where to search for compilation databases for this file's flags.
struct clang::clangd::Config::@2 CompileFlags
Controls how the compile command for the current file is determined.