Skip to content

Commit a65bcbf

Browse files
committedJan 22, 2019
[clangd] NFC: Use buildCompilerInvocation in CodeComplete
Reviewers: ilya-biryukov, sammccall Reviewed By: sammccall Subscribers: ioeric, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D56860 llvm-svn: 351793
1 parent 1ca0c58 commit a65bcbf

File tree

5 files changed

+46
-59
lines changed

5 files changed

+46
-59
lines changed
 

‎clang-tools-extra/clangd/ClangdUnit.cpp

-28
Original file line numberDiff line numberDiff line change
@@ -418,34 +418,6 @@ ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble,
418418
assert(this->Action);
419419
}
420420

421-
std::unique_ptr<CompilerInvocation>
422-
buildCompilerInvocation(const ParseInputs &Inputs) {
423-
std::vector<const char *> ArgStrs;
424-
for (const auto &S : Inputs.CompileCommand.CommandLine)
425-
ArgStrs.push_back(S.c_str());
426-
427-
if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
428-
log("Couldn't set working directory when creating compiler invocation.");
429-
// We proceed anyway, our lit-tests rely on results for non-existing working
430-
// dirs.
431-
}
432-
433-
// FIXME(ibiryukov): store diagnostics from CommandLine when we start
434-
// reporting them.
435-
IgnoreDiagnostics IgnoreDiagnostics;
436-
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
437-
CompilerInstance::createDiagnostics(new DiagnosticOptions,
438-
&IgnoreDiagnostics, false);
439-
std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
440-
ArgStrs, CommandLineDiagsEngine, Inputs.FS);
441-
if (!CI)
442-
return nullptr;
443-
// createInvocationFromCommandLine sets DisableFree.
444-
CI->getFrontendOpts().DisableFree = false;
445-
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
446-
return CI;
447-
}
448-
449421
std::shared_ptr<const PreambleData>
450422
buildPreamble(PathRef FileName, CompilerInvocation &CI,
451423
std::shared_ptr<const PreambleData> OldPreamble,

‎clang-tools-extra/clangd/ClangdUnit.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H
1111

12-
#include "../clang-tidy/ClangTidyOptions.h"
12+
#include "Compiler.h"
1313
#include "Diagnostics.h"
1414
#include "FS.h"
1515
#include "Function.h"
@@ -60,14 +60,6 @@ struct PreambleData {
6060
std::unique_ptr<PreambleFileStatusCache> StatCache;
6161
};
6262

63-
/// Information required to run clang, e.g. to parse AST or do code completion.
64-
struct ParseInputs {
65-
tooling::CompileCommand CompileCommand;
66-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
67-
std::string Contents;
68-
tidy::ClangTidyOptions ClangTidyOpts;
69-
};
70-
7163
/// Stores and provides access to parsed AST.
7264
class ParsedAST {
7365
public:
@@ -137,10 +129,6 @@ class ParsedAST {
137129
using PreambleParsedCallback =
138130
std::function<void(ASTContext &, std::shared_ptr<clang::Preprocessor>)>;
139131

140-
/// Builds compiler invocation that could be used to build AST or preamble.
141-
std::unique_ptr<CompilerInvocation>
142-
buildCompilerInvocation(const ParseInputs &Inputs);
143-
144132
/// Rebuild the preamble for the new inputs unless the old one can be reused.
145133
/// If \p OldPreamble can be reused, it is returned unchanged.
146134
/// If \p OldPreamble is null, always builds the preamble.

‎clang-tools-extra/clangd/CodeComplete.cpp

+3-18
Original file line numberDiff line numberDiff line change
@@ -1016,33 +1016,17 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
10161016
const SemaCompleteInput &Input,
10171017
IncludeStructure *Includes = nullptr) {
10181018
trace::Span Tracer("Sema completion");
1019-
std::vector<const char *> ArgStrs;
1020-
for (const auto &S : Input.Command.CommandLine)
1021-
ArgStrs.push_back(S.c_str());
1022-
1023-
if (Input.VFS->setCurrentWorkingDirectory(Input.Command.Directory)) {
1024-
log("Couldn't set working directory");
1025-
// We run parsing anyway, our lit-tests rely on results for non-existing
1026-
// working dirs.
1027-
}
1028-
10291019
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = Input.VFS;
10301020
if (Input.Preamble && Input.Preamble->StatCache)
10311021
VFS = Input.Preamble->StatCache->getConsumingFS(std::move(VFS));
1032-
IgnoreDiagnostics DummyDiagsConsumer;
1033-
auto CI = createInvocationFromCommandLine(
1034-
ArgStrs,
1035-
CompilerInstance::createDiagnostics(new DiagnosticOptions,
1036-
&DummyDiagsConsumer, false),
1037-
VFS);
1022+
auto CI =
1023+
buildCompilerInvocation(ParseInputs{Input.Command, VFS, Input.Contents});
10381024
if (!CI) {
10391025
elog("Couldn't create CompilerInvocation");
10401026
return false;
10411027
}
10421028
auto &FrontendOpts = CI->getFrontendOpts();
1043-
FrontendOpts.DisableFree = false;
10441029
FrontendOpts.SkipFunctionBodies = true;
1045-
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
10461030
// Disable typo correction in Sema.
10471031
CI->getLangOpts()->SpellChecking = false;
10481032
// Setup code completion.
@@ -1072,6 +1056,7 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
10721056
*Offset;
10731057
// NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
10741058
// the remapped buffers do not get freed.
1059+
IgnoreDiagnostics DummyDiagsConsumer;
10751060
auto Clang = prepareCompilerInstance(
10761061
std::move(CI),
10771062
(Input.Preamble && !CompletingInPreamble) ? &Input.Preamble->Preamble

‎clang-tools-extra/clangd/Compiler.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
3939
IgnoreDiagnostics::log(DiagLevel, Info);
4040
}
4141

42+
std::unique_ptr<CompilerInvocation>
43+
buildCompilerInvocation(const ParseInputs &Inputs) {
44+
std::vector<const char *> ArgStrs;
45+
for (const auto &S : Inputs.CompileCommand.CommandLine)
46+
ArgStrs.push_back(S.c_str());
47+
48+
if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
49+
log("Couldn't set working directory when creating compiler invocation.");
50+
// We proceed anyway, our lit-tests rely on results for non-existing working
51+
// dirs.
52+
}
53+
54+
// FIXME(ibiryukov): store diagnostics from CommandLine when we start
55+
// reporting them.
56+
IgnoreDiagnostics IgnoreDiagnostics;
57+
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
58+
CompilerInstance::createDiagnostics(new DiagnosticOptions,
59+
&IgnoreDiagnostics, false);
60+
std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
61+
ArgStrs, CommandLineDiagsEngine, Inputs.FS);
62+
if (!CI)
63+
return nullptr;
64+
// createInvocationFromCommandLine sets DisableFree.
65+
CI->getFrontendOpts().DisableFree = false;
66+
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
67+
return CI;
68+
}
69+
4270
std::unique_ptr<CompilerInstance>
4371
prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
4472
const PrecompiledPreamble *Preamble,

‎clang-tools-extra/clangd/Compiler.h

+14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
1616
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
1717

18+
#include "../clang-tidy/ClangTidyOptions.h"
1819
#include "clang/Frontend/CompilerInstance.h"
1920
#include "clang/Frontend/CompilerInvocation.h"
2021
#include "clang/Frontend/PrecompiledPreamble.h"
22+
#include "clang/Tooling/CompilationDatabase.h"
2123

2224
namespace clang {
2325
namespace clangd {
@@ -31,6 +33,18 @@ class IgnoreDiagnostics : public DiagnosticConsumer {
3133
const clang::Diagnostic &Info) override;
3234
};
3335

36+
/// Information required to run clang, e.g. to parse AST or do code completion.
37+
struct ParseInputs {
38+
tooling::CompileCommand CompileCommand;
39+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
40+
std::string Contents;
41+
tidy::ClangTidyOptions ClangTidyOpts;
42+
};
43+
44+
/// Builds compiler invocation that could be used to build AST or preamble.
45+
std::unique_ptr<CompilerInvocation>
46+
buildCompilerInvocation(const ParseInputs &Inputs);
47+
3448
/// Creates a compiler instance, configured so that:
3549
/// - Contents of the parsed file are remapped to \p MainFile.
3650
/// - Preamble is overriden to use PCH passed to this function. It means the

0 commit comments

Comments
 (0)