From 649282648a237c63c3c781948b37043067949478 Mon Sep 17 00:00:00 2001 From: i-ky Date: Tue, 4 Apr 2023 19:33:05 +0000 Subject: [PATCH] Use typical source file extensions to detect source files among command arguments --- src/basset.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/basset.cpp b/src/basset.cpp index 7041f0c..ae4e887 100644 --- a/src/basset.cpp +++ b/src/basset.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ using std::ofstream; using std::ostream; using std::string; using std::to_string; +using std::unordered_set; using std::vector; using std::literals::string_literals::operator""s; @@ -158,6 +160,7 @@ class CompilationDatabase : ofstream { void add(const string &directory, const vector &command); private: + [[nodiscard]] static bool is_source_file(const string &argument); bool first{true}; }; @@ -177,7 +180,11 @@ void CompilationDatabase::add(const string &directory, vector files; - files.emplace_back(command.back()); // FIXME + for (auto &&argument : command) { + if (is_source_file(argument)) { + files.emplace_back(argument); + } + } for (const auto &file : files) { *this << "\n" @@ -213,6 +220,25 @@ void CompilationDatabase::add(const string &directory, CompilationDatabase::~CompilationDatabase() { *this << "\n]\n"; } +bool CompilationDatabase::is_source_file(const string &argument) { + // file extensions associated with C, C++, Objective-C, Objective-C++ + // https://github.com/github/linguist/blob/master/lib/linguist/languages.yml + static const unordered_set extensions{ + "c"s, "cats"s, "h"s, "idc"s, "cpp"s, "c++"s, "cc"s, "cp"s, + "cppm"s, "cxx"s, "h++"s, "hh"s, "hpp"s, "hxx"s, "inc"s, "inl"s, + "ino"s, "ipp"s, "ixx"s, "re"s, "tcc"s, "tpp"s, "m"s, "mm"s}; + + auto dot = argument.find_last_of('.'); + + if (dot == string::npos) { + return false; + } + + auto extension = argument.substr(dot + 1); + + return extensions.count(extension) != 0; +} + int main(int argc, char *argv[]) { using token = char;