Support clang-cl and cl.exe

This commit is contained in:
Fangrui Song 2018-02-20 23:52:49 -08:00
parent 1a4da727da
commit 20d1636024
2 changed files with 37 additions and 34 deletions

View File

@ -370,7 +370,7 @@ void TryEnsureDocumentParsed(ClangCompleteManager* manager,
std::vector<CXUnsavedFile> unsaved = snapshot.AsUnsavedFiles(); std::vector<CXUnsavedFile> unsaved = snapshot.AsUnsavedFiles();
LOG_S(INFO) << "Creating completion session with arguments " LOG_S(INFO) << "Creating completion session with arguments "
<< StringJoin(args); << StringJoin(args, " ");
*tu = ClangTranslationUnit::Create(index, session->file.filename, args, *tu = ClangTranslationUnit::Create(index, session->file.filename, args,
unsaved, Flags()); unsaved, Flags());

View File

@ -133,16 +133,18 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
Project::Entry result; Project::Entry result;
result.filename = NormalizePathWithTestOptOut(entry.file); result.filename = NormalizePathWithTestOptOut(entry.file);
if (entry.args.empty())
return result;
std::string base_name = GetBaseName(entry.file); std::string base_name = GetBaseName(entry.file);
size_t i = 0; bool clang_cl = strstr(entry.args[0].c_str(), "clang-cl") ||
strstr(entry.args[0].c_str(), "cl.exe") ||
AnyStartsWith(entry.args, "--driver-mode=cl");
size_t i = 1;
// If |compilationDatabaseCommand| is specified, the external command provides // If |compilationDatabaseCommand| is specified, the external command provides
// us the JSON compilation database which should be strict. We should do very // us the JSON compilation database which should be strict. We should do very
// little processing on |entry.args|. // little processing on |entry.args|.
if (config->mode == ProjectMode::ExternalCommand) { if (config->mode != ProjectMode::ExternalCommand && !clang_cl) {
if (entry.args.size())
i = 1;
} else {
// Strip all arguments consisting the compiler command, // Strip all arguments consisting the compiler command,
// as there may be non-compiler related commands beforehand, // as there may be non-compiler related commands beforehand,
// ie, compiler schedular such as goma. This allows correct parsing for // ie, compiler schedular such as goma. This allows correct parsing for
@ -163,27 +165,23 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
++i; ++i;
} }
// Compiler driver. // Compiler driver.
if (i > 0) result.args.push_back(entry.args[i - 1]);
result.args.push_back(entry.args[i - 1]);
// Add -working-directory if not provided. // Add -working-directory if not provided.
if (!AnyStartsWith(entry.args, "-working-directory")) { if (!AnyStartsWith(entry.args, "-working-directory"))
result.args.emplace_back("-working-directory"); result.args.emplace_back("-working-directory=" + entry.directory);
result.args.push_back(entry.directory);
}
if (config->mode == ProjectMode::DotCquery && if (config->mode == ProjectMode::DotCquery &&
!AnyStartsWith(entry.args, "-std=") && !AnyStartsWith(entry.args, "-std=")) {
!AnyStartsWith(entry.args, "--driver-mode=")) {
switch (SourceFileLanguage(entry.file)) { switch (SourceFileLanguage(entry.file)) {
case LanguageId::C: case LanguageId::C:
result.args.push_back("-std=gnu11"); result.args.push_back("-std=gnu11");
break; break;
case LanguageId::Cpp: case LanguageId::Cpp:
result.args.push_back("-std=gnu++14"); result.args.push_back("-std=gnu++14");
break; break;
default: default:
break; break;
} }
} }
@ -342,18 +340,23 @@ std::vector<Project::Entry> LoadFromDirectoryListing(Config* init_opts,
e.file = file; e.file = file;
e.args = GetCompilerArgumentForFile(file); e.args = GetCompilerArgumentForFile(file);
e.args.push_back(e.file); e.args.push_back(e.file);
switch (SourceFileLanguage(e.file)) { auto idx = e.args[0].rfind("clang");
case LanguageId::C: if (idx != std::string::npos) {
// g++ or clang++ idx += 5;
if (e.args[0].find("++") != std::string::npos) switch (SourceFileLanguage(e.file)) {
e.args[0] = "clang"; case LanguageId::C:
break; if (e.args[0].compare(idx, 2, "++") == 0)
case LanguageId::Cpp: e.args[0].erase(idx, 2);
if (e.args[0].find("++") == std::string::npos) break;
e.args[0] = "clang++"; case LanguageId::Cpp:
break; // Neither clang++ nor clang-cl
default: if (e.args[0].compare(idx, 2, "++") &&
break; e.args[0].compare(idx, 3, "-cl"))
e.args[0].insert(idx, "++");
break;
default:
break;
}
} }
result.push_back( result.push_back(
GetCompilationEntryFromCompileCommandEntry(init_opts, config, e)); GetCompilationEntryFromCompileCommandEntry(init_opts, config, e));