Skip to content

Commit

Permalink
Support clang-cl and cl.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Feb 21, 2018
1 parent c6c0a83 commit e15f57f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/clang_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void TryEnsureDocumentParsed(ClangCompleteManager* manager,
std::vector<CXUnsavedFile> unsaved = snapshot.AsUnsavedFiles();

LOG_S(INFO) << "Creating completion session with arguments "
<< StringJoin(args);
<< StringJoin(args, " ");
*tu = ClangTranslationUnit::Create(index, session->file.filename, args,
unsaved, Flags());

Expand Down
69 changes: 36 additions & 33 deletions src/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,18 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(

Project::Entry result;
result.filename = NormalizePathWithTestOptOut(entry.file);
if (entry.args.empty())
return result;
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
// us the JSON compilation database which should be strict. We should do very
// little processing on |entry.args|.
if (config->mode == ProjectMode::ExternalCommand) {
if (entry.args.size())
i = 1;
} else {
if (config->mode != ProjectMode::ExternalCommand && !clang_cl) {
// Strip all arguments consisting the compiler command,
// as there may be non-compiler related commands beforehand,
// ie, compiler schedular such as goma. This allows correct parsing for
Expand All @@ -163,27 +165,23 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
++i;
}
// 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.
if (!AnyStartsWith(entry.args, "-working-directory")) {
result.args.emplace_back("-working-directory");
result.args.push_back(entry.directory);
}
if (!AnyStartsWith(entry.args, "-working-directory"))
result.args.emplace_back("-working-directory=" + entry.directory);

if (config->mode == ProjectMode::DotCquery &&
!AnyStartsWith(entry.args, "-std=") &&
!AnyStartsWith(entry.args, "--driver-mode=")) {
!AnyStartsWith(entry.args, "-std=")) {
switch (SourceFileLanguage(entry.file)) {
case LanguageId::C:
result.args.push_back("-std=gnu11");
break;
case LanguageId::Cpp:
result.args.push_back("-std=gnu++14");
break;
default:
break;
case LanguageId::C:
result.args.push_back("-std=gnu11");
break;
case LanguageId::Cpp:
result.args.push_back("-std=gnu++14");
break;
default:
break;
}
}

Expand Down Expand Up @@ -342,18 +340,23 @@ std::vector<Project::Entry> LoadFromDirectoryListing(Config* init_opts,
e.file = file;
e.args = GetCompilerArgumentForFile(file);
e.args.push_back(e.file);
switch (SourceFileLanguage(e.file)) {
case LanguageId::C:
// g++ or clang++
if (e.args[0].find("++") != std::string::npos)
e.args[0] = "clang";
break;
case LanguageId::Cpp:
if (e.args[0].find("++") == std::string::npos)
e.args[0] = "clang++";
break;
default:
break;
auto idx = e.args[0].rfind("clang");
if (idx != std::string::npos) {
idx += 5;
switch (SourceFileLanguage(e.file)) {
case LanguageId::C:
if (e.args[0].compare(idx, 2, "++") == 0)
e.args[0].erase(idx, 2);
break;
case LanguageId::Cpp:
// Neither clang++ nor clang-cl
if (e.args[0].compare(idx, 2, "++") &&
e.args[0].compare(idx, 3, "-cl"))
e.args[0].insert(idx, "++");
break;
default:
break;
}
}
result.push_back(
GetCompilationEntryFromCompileCommandEntry(init_opts, config, e));
Expand Down

0 comments on commit e15f57f

Please sign in to comment.