2626#include < algorithm>
2727#include < cstdlib>
2828#include < sys/stat.h>
29+ #include < unordered_set>
2930#include < utility>
3031
3132#include < simplecpp.h>
@@ -193,6 +194,18 @@ std::string Path::getRelativePath(const std::string& absolutePath, const std::ve
193194 return absolutePath;
194195}
195196
197+ static const std::unordered_set<std::string> cpp_src_exts = {
198+ " .cpp" , " .cxx" , " .cc" , " .c++" , " .tpp" , " .txx" , " .ipp" , " .ixx"
199+ };
200+
201+ static const std::unordered_set<std::string> c_src_exts = {
202+ " .c" , " .cl"
203+ };
204+
205+ static const std::unordered_set<std::string> header_exts = {
206+ " .h" , " .hpp" , " .h++" , " .hxx" , " .hh"
207+ };
208+
196209bool Path::isC (const std::string &path)
197210{
198211 // In unix, ".C" is considered C++ file
@@ -220,7 +233,8 @@ bool Path::isCPP(const std::string &path)
220233
221234bool Path::acceptFile (const std::string &path, const std::set<std::string> &extra)
222235{
223- return !Path::isHeader (path) && (Path::isCPP (path) || Path::isC (path) || extra.find (getFilenameExtension (path)) != extra.end ());
236+ bool header = false ;
237+ return (identify (path, &header) != Standards::Language::None && !header) || extra.find (getFilenameExtension (path)) != extra.end ();
224238}
225239
226240bool Path::isHeader (const std::string &path)
@@ -229,6 +243,33 @@ bool Path::isHeader(const std::string &path)
229243 return startsWith (extension, " .h" );
230244}
231245
246+ Standards::Language Path::identify (const std::string &path, bool *header)
247+ {
248+ if (header)
249+ *header = false ;
250+
251+ std::string ext = getFilenameExtension (path);
252+ if (ext == " .C" )
253+ return Standards::Language::CPP;
254+ if (c_src_exts.find (ext) != c_src_exts.end ())
255+ return Standards::Language::C;
256+ if (!caseInsensitiveFilesystem ())
257+ strTolower (ext);
258+ if (ext == " .h" ) {
259+ if (header)
260+ *header = true ;
261+ return Standards::Language::C; // treat as C for now
262+ }
263+ if (cpp_src_exts.find (ext) != cpp_src_exts.end ())
264+ return Standards::Language::CPP;
265+ if (header_exts.find (ext) != header_exts.end ()) {
266+ if (header)
267+ *header = true ;
268+ return Standards::Language::CPP;
269+ }
270+ return Standards::Language::None;
271+ }
272+
232273std::string Path::getAbsoluteFilePath (const std::string& filePath)
233274{
234275 std::string absolute_path;
0 commit comments