Skip to content

Commit

Permalink
Infer system include paths from CompilerInvocation
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Jun 4, 2018
1 parent 0decb01 commit 1341266
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 37 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ target_sources(ccls PRIVATE
src/clang_tu.cc
src/clang_utils.cc
src/config.cc
src/diagnostics_engine.cc
src/diagnostics_publisher.cc
src/file_consumer.cc
src/filesystem.cc
src/fuzzy_match.cc
Expand Down
6 changes: 5 additions & 1 deletion cmake/FindClang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,20 @@ set(_Clang_REQUIRED_VARS Clang_LIBRARY Clang_INCLUDE_DIR Clang_EXECUTABLE
LLVM_INCLUDE_DIR LLVM_BUILD_INCLUDE_DIR)

_Clang_find_library(Clang_LIBRARY clang)
_Clang_find_add_library(clangFrontend)
_Clang_find_add_library(clangSerialization)
_Clang_find_add_library(clangAST)
_Clang_find_add_library(clangLex)
_Clang_find_add_library(clangDriver)
_Clang_find_add_library(clangBasic)
if(USE_SHARED_LLVM)
_Clang_find_add_library(LLVM)
else()
_Clang_find_add_library(LLVMBitReader)
_Clang_find_add_library(LLVMOption)
_Clang_find_add_library(LLVMProfileData)
_Clang_find_add_library(LLVMCore)
_Clang_find_add_library(LLVMBinaryFormat)
_Clang_find_add_library(LLVMOption)
_Clang_find_add_library(LLVMSupport)
_Clang_find_add_library(LLVMDemangle)
endif()
Expand Down
10 changes: 5 additions & 5 deletions src/diagnostics_engine.cc → src/diagnostics_publisher.cc
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "diagnostics_engine.h"
#include "diagnostics_publisher.hh"

#include "pipeline.hh"
using namespace ccls;

#include <chrono>

void DiagnosticsEngine::Init() {
void DiagnosticsPublisher::Init() {
frequencyMs_ = g_config->diagnostics.frequencyMs;
match_ = std::make_unique<GroupMatch>(g_config->diagnostics.whitelist,
g_config->diagnostics.blacklist);
}

void DiagnosticsEngine::Publish(WorkingFiles* working_files,
std::string path,
std::vector<lsDiagnostic> diagnostics) {
void DiagnosticsPublisher::Publish(WorkingFiles* working_files,
std::string path,
std::vector<lsDiagnostic> diagnostics) {
// Cache diagnostics so we can show fixits.
working_files->DoActionOnFile(path, [&](WorkingFile* working_file) {
if (working_file)
Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics_engine.h → src/diagnostics_publisher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "match.h"
#include "working_files.h"

class DiagnosticsEngine {
class DiagnosticsPublisher {
std::unique_ptr<GroupMatch> match_;
int64_t nextPublish_ = 0;
int frequencyMs_;
Expand Down
5 changes: 0 additions & 5 deletions src/indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2070,9 +2070,6 @@ std::vector<std::unique_ptr<IndexFile>> ParseWithTu(
const std::string& file,
const std::vector<std::string>& args,
const std::vector<CXUnsavedFile>& file_contents) {
Timer timer;
timer.startTimer();

IndexerCallbacks callback = {0};
// Available callbacks:
// - abortQuery
Expand Down Expand Up @@ -2114,8 +2111,6 @@ std::vector<std::unique_ptr<IndexFile>> ParseWithTu(
ClangCursor(clang_getTranslationUnitCursor(tu->cx_tu))
.VisitChildren(&VisitMacroDefinitionAndExpansions, &param);

timer.stopTimer();

std::unordered_map<std::string, int> inc_to_line;
// TODO
if (param.primary_file)
Expand Down
4 changes: 2 additions & 2 deletions src/message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
struct ClangCompleteManager;
struct CodeCompleteCache;
struct Config;
class DiagnosticsEngine;
class DiagnosticsPublisher;
struct VFS;
struct ImportManager;
struct IncludeComplete;
Expand Down Expand Up @@ -104,7 +104,7 @@ struct MessageHandler {
DB* db = nullptr;
MultiQueueWaiter* waiter = nullptr;
Project* project = nullptr;
DiagnosticsEngine* diag_engine = nullptr;
DiagnosticsPublisher* diag_pub = nullptr;
VFS* vfs = nullptr;
ImportManager* import_manager = nullptr;
SemanticHighlightSymbolCache* semantic_cache = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/messages/initialize.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "diagnostics_engine.h"
#include "diagnostics_publisher.hh"
#include "filesystem.hh"
#include "include_complete.h"
#include "log.hh"
Expand Down Expand Up @@ -492,7 +492,7 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
sys::fs::create_directories(g_config->cacheDirectory + '@' +
EscapeFileName(g_config->projectRoot));

diag_engine->Init();
diag_pub->Init();
semantic_cache->Init();

// Open up / load the project.
Expand All @@ -510,7 +510,7 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
g_thread_id = i + 1;
std::string name = "indexer" + std::to_string(i);
set_thread_name(name.c_str());
pipeline::Indexer_Main(diag_engine, vfs, project, working_files);
pipeline::Indexer_Main(diag_pub, vfs, project, working_files);
}).detach();
}

Expand Down
16 changes: 8 additions & 8 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "clang_complete.h"
#include "config.h"
#include "diagnostics_engine.h"
#include "diagnostics_publisher.hh"
#include "include_complete.h"
#include "log.hh"
#include "lsp.h"
Expand Down Expand Up @@ -119,7 +119,7 @@ std::unique_ptr<IndexFile> RawCacheLoad(
*file_content, IndexFile::kMajorVersion);
}

bool Indexer_Parse(DiagnosticsEngine* diag_engine,
bool Indexer_Parse(DiagnosticsPublisher* diag_pub,
WorkingFiles* working_files,
Project* project,
VFS* vfs,
Expand Down Expand Up @@ -224,7 +224,7 @@ bool Indexer_Parse(DiagnosticsEngine* diag_engine,
// to identify indexing problems. For interactive sessions, diagnostics are
// handled by code completion.
if (!request.is_interactive)
diag_engine->Publish(working_files, curr->path, curr->diagnostics_);
diag_pub->Publish(working_files, curr->path, curr->diagnostics_);

std::string path = curr->path;
if (!(vfs->Stamp(path, curr->last_write_time) || path == path_to_index))
Expand Down Expand Up @@ -275,15 +275,15 @@ void Init() {
for_stdout = new ThreadedQueue<Stdout_Request>(stdout_waiter);
}

void Indexer_Main(DiagnosticsEngine* diag_engine,
void Indexer_Main(DiagnosticsPublisher* diag_pub,
VFS* vfs,
Project* project,
WorkingFiles* working_files) {
// Build one index per-indexer, as building the index acquires a global lock.
ClangIndexer indexer;

while (true)
if (!Indexer_Parse(diag_engine, working_files, project, vfs, &indexer))
if (!Indexer_Parse(diag_pub, working_files, project, vfs, &indexer))
indexer_waiter->Wait(index_request);
}

Expand Down Expand Up @@ -386,12 +386,12 @@ void MainLoop() {
SemanticHighlightSymbolCache semantic_cache;
WorkingFiles working_files;
VFS vfs;
DiagnosticsEngine diag_engine;
DiagnosticsPublisher diag_pub;

ClangCompleteManager clang_complete(
&project, &working_files,
[&](std::string path, std::vector<lsDiagnostic> diagnostics) {
diag_engine.Publish(&working_files, path, diagnostics);
diag_pub.Publish(&working_files, path, diagnostics);
},
[](lsRequestId id) {
if (id.Valid()) {
Expand All @@ -416,7 +416,7 @@ void MainLoop() {
handler->db = &db;
handler->waiter = indexer_waiter;
handler->project = &project;
handler->diag_engine = &diag_engine;
handler->diag_pub = &diag_pub;
handler->vfs = &vfs;
handler->semantic_cache = &semantic_cache;
handler->working_files = &working_files;
Expand Down
4 changes: 2 additions & 2 deletions src/pipeline.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <unordered_map>
#include <vector>

class DiagnosticsEngine;
class DiagnosticsPublisher;
struct VFS;
struct Project;
struct WorkingFiles;
Expand All @@ -18,7 +18,7 @@ namespace ccls::pipeline {
void Init();
void LaunchStdin();
void LaunchStdout();
void Indexer_Main(DiagnosticsEngine* diag_engine,
void Indexer_Main(DiagnosticsPublisher* diag_pub,
VFS* vfs,
Project* project,
WorkingFiles* working_files);
Expand Down
67 changes: 58 additions & 9 deletions src/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#include "working_files.h"
using namespace ccls;

#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Driver/Options.h>
#include <clang/Frontend/CompilerInstance.h>
#include <llvm/ADT/ArrayRef.h>
#include <llvm/Option/ArgList.h>
#include <llvm/Option/OptTable.h>
Expand Down Expand Up @@ -97,6 +100,7 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
args.insert(args.end(), config->extra_flags.begin(),
config->extra_flags.end());

#if 1
std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
unsigned MissingArgIndex, MissingArgCount;
std::vector<const char*> cargs;
Expand All @@ -105,18 +109,63 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
InputArgList Args =
Opts->ParseArgs(makeArrayRef(cargs), MissingArgIndex, MissingArgCount,
driver::options::CC1Option);

using namespace clang::driver::options;
for (const auto* A :
Args.filtered(OPT_I, OPT_c_isystem, OPT_cxx_isystem, OPT_isystem))
for (const auto* A : Args.filtered(OPT_I, OPT_c_isystem, OPT_cxx_isystem,
OPT_isystem, OPT_idirafter))
config->angle_dirs.insert(entry.ResolveIfRelative(A->getValue()));
for (const auto* A : Args.filtered(OPT_I, OPT_iquote))
config->quote_dirs.insert(entry.ResolveIfRelative(A->getValue()));
for (const auto* A : Args.filtered(OPT_idirafter)) {
std::string dir = entry.ResolveIfRelative(A->getValue());
config->angle_dirs.insert(dir);
config->quote_dirs.insert(dir);
#else
// a weird C++ deduction guide heap-use-after-free causes libclang to crash.
IgnoringDiagConsumer DiagC;
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
DiagnosticsEngine Diags(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
&DiagC, false);
driver::Driver Driver(args[0], llvm::sys::getDefaultTargetTriple(), Diags);
auto TargetAndMode =
driver::ToolChain::getTargetAndModeFromProgramName(args[0]);
if (!TargetAndMode.TargetPrefix.empty()) {
const char* arr[] = {"-target", TargetAndMode.TargetPrefix.c_str()};
args.insert(args.begin() + 1, std::begin(arr), std::end(arr));
Driver.setTargetAndMode(TargetAndMode);
}
Driver.setCheckInputsExist(false);
std::vector<const char*> cargs;
for (auto& arg : args)
cargs.push_back(arg.c_str());
cargs.push_back("-fsyntax-only");
std::unique_ptr<driver::Compilation> C(Driver.BuildCompilation(cargs));
const driver::JobList& Jobs = C->getJobs();
if (Jobs.size() != 1)
return result;
const driver::ArgStringList& CCArgs = Jobs.begin()->getArguments();
auto Invocation = std::make_unique<CompilerInvocation>();
CompilerInvocation::CreateFromArgs(*Invocation, CCArgs.data(),
CCArgs.data() + CCArgs.size(), Diags);
Invocation->getFrontendOpts().DisableFree = false;
Invocation->getCodeGenOpts().DisableFree = false;
HeaderSearchOptions& HeaderOpts = Invocation->getHeaderSearchOpts();
for (auto& E : HeaderOpts.UserEntries) {
std::string path = entry.ResolveIfRelative(E.Path);
switch (E.Group) {
default:
config->angle_dirs.insert(path);
break;
case frontend::Quoted:
config->quote_dirs.insert(path);
break;
case frontend::Angled:
config->angle_dirs.insert(path);
config->quote_dirs.insert(path);
break;
}
}
#endif

for (size_t i = 1; i < args.size(); i++)
// This is most likely the file path we will be passing to clang. The
Expand All @@ -128,9 +177,9 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
continue;
}

if (!Args.hasArg(OPT_resource_dir))
// if (HeaderOpts.ResourceDir.empty() && HeaderOpts.UseBuiltinIncludes)
args.push_back("-resource-dir=" + g_config->clang.resourceDir);
if (!Args.hasArg(OPT_working_directory))
// if (Invocation->getFileSystemOpts().WorkingDir.empty())
args.push_back("-working-directory=" + entry.directory);

// There could be a clang version mismatch between what the project uses and
Expand Down

0 comments on commit 1341266

Please sign in to comment.