From 440fa950a0b2022f66677aa6a06c7b040f8cfbd0 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sun, 21 May 2017 23:06:30 -0700 Subject: [PATCH] Use -xc (instead of -xc++) for files ending with .c. A good chunk of c files will fail to compile under c++ mode. --- src/project.cc | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/project.cc b/src/project.cc index 271ea857..e09c3da0 100644 --- a/src/project.cc +++ b/src/project.cc @@ -113,6 +113,12 @@ bool ShouldAddToAngleIncludes(const std::string& arg) { return false; } +// Returns true if we should use the C, not C++, language spec for the given +// file. +bool IsCFile(const std::string& path) { + return EndsWith(path, ".c"); +} + Project::Entry GetCompilationEntryFromCompileCommandEntry( std::unordered_set& quote_includes, std::unordered_set& angle_includes, const std::vector& extra_flags, const CompileCommandsEntry& entry) { @@ -187,12 +193,20 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( for (const auto& flag : extra_flags) result.args.push_back(flag); - // Clang does not have good hueristics for determining source language. We - // default to C++11 if the user has not specified. - if (!AnyStartsWith(result.args, "-x")) - result.args.push_back("-xc++"); - if (!AnyStartsWith(result.args, "-std=")) - result.args.push_back("-std=c++11"); + // Clang does not have good hueristics for determining source language, we + // should explicitly specify it. + if (!AnyStartsWith(result.args, "-x")) { + if (IsCFile(entry.file)) + result.args.push_back("-xc"); + else + result.args.push_back("-xc++"); + } + if (!AnyStartsWith(result.args, "-std=")) { + if (IsCFile(entry.file)) + result.args.push_back("-std=c11"); + else + result.args.push_back("-std=c++11"); + } return result; }