Refactor file extension queries to be more consistent

It was very easy to forgot to check against all language file
extensions. This updates the internal API to have a unified API.
This commit is contained in:
Robert Maynard
2020-06-15 09:23:49 -04:00
parent 6f7853cb42
commit 7628153edb
10 changed files with 53 additions and 61 deletions
+1 -1
View File
@@ -55,7 +55,7 @@ bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
auto ext = cm::string_view(file).substr(dotpos + 1); auto ext = cm::string_view(file).substr(dotpos + 1);
// Process only source files // Process only source files
auto cm = mf.GetCMakeInstance(); auto cm = mf.GetCMakeInstance();
if (dotpos > 0 && cm->IsSourceExtension(ext)) { if (dotpos > 0 && cm->IsACLikeSourceExtension(ext)) {
std::string fullname = cmStrCat(templateDirectory, '/', file); std::string fullname = cmStrCat(templateDirectory, '/', file);
// add the file as a class file so // add the file as a class file so
// depends can be done // depends can be done
+1 -1
View File
@@ -370,7 +370,7 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
std::string lang = s->GetOrDetermineLanguage(); std::string lang = s->GetOrDetermineLanguage();
if (lang == "C" || lang == "CXX" || lang == "CUDA") { if (lang == "C" || lang == "CXX" || lang == "CUDA") {
std::string const& srcext = s->GetExtension(); std::string const& srcext = s->GetExtension();
isCFile = cm->IsSourceExtension(srcext); isCFile = cm->IsACLikeSourceExtension(srcext);
} }
std::string const& fullPath = s->ResolveFullPath(); std::string const& fullPath = s->ResolveFullPath();
+1 -2
View File
@@ -227,8 +227,7 @@ std::string cmExtraCodeLiteGenerator::CollectSourceFiles(
cmSystemTools::LowerCase(s->GetExtension()); cmSystemTools::LowerCase(s->GetExtension());
// check whether it is a source or a include file // check whether it is a source or a include file
// then put it accordingly into one of the two containers // then put it accordingly into one of the two containers
if (cm->IsSourceExtension(extLower) || cm->IsCudaExtension(extLower) || if (cm->IsAKnownSourceExtension(extLower)) {
cm->IsFortranExtension(extLower)) {
cFiles[fullPath] = s; cFiles[fullPath] = s;
} else { } else {
otherFiles.insert(fullPath); otherFiles.insert(fullPath);
+4 -4
View File
@@ -789,9 +789,9 @@ bool cmQtAutoGenInitializer::InitScanFiles()
// Register files that will be scanned by moc or uic // Register files that will be scanned by moc or uic
if (this->MocOrUicEnabled()) { if (this->MocOrUicEnabled()) {
if (cm->IsHeaderExtension(extLower)) { if (cm->IsAHeaderExtension(extLower)) {
addMUHeader(makeMUFile(sf, fullPath, true), extLower); addMUHeader(makeMUFile(sf, fullPath, true), extLower);
} else if (cm->IsSourceExtension(extLower)) { } else if (cm->IsACLikeSourceExtension(extLower)) {
addMUSource(makeMUFile(sf, fullPath, true)); addMUSource(makeMUFile(sf, fullPath, true));
} }
} }
@@ -895,14 +895,14 @@ bool cmQtAutoGenInitializer::InitScanFiles()
std::string const& extLower = std::string const& extLower =
cmSystemTools::LowerCase(sf->GetExtension()); cmSystemTools::LowerCase(sf->GetExtension());
if (cm->IsHeaderExtension(extLower)) { if (cm->IsAHeaderExtension(extLower)) {
if (!cm::contains(this->AutogenTarget.Headers, sf.get())) { if (!cm::contains(this->AutogenTarget.Headers, sf.get())) {
auto muf = makeMUFile(sf.get(), fullPath, false); auto muf = makeMUFile(sf.get(), fullPath, false);
if (muf->SkipMoc || muf->SkipUic) { if (muf->SkipMoc || muf->SkipUic) {
addMUHeader(std::move(muf), extLower); addMUHeader(std::move(muf), extLower);
} }
} }
} else if (cm->IsSourceExtension(extLower)) { } else if (cm->IsACLikeSourceExtension(extLower)) {
if (!cm::contains(this->AutogenTarget.Sources, sf.get())) { if (!cm::contains(this->AutogenTarget.Sources, sf.get())) {
auto muf = makeMUFile(sf.get(), fullPath, false); auto muf = makeMUFile(sf.get(), fullPath, false);
if (muf->SkipMoc || muf->SkipUic) { if (muf->SkipMoc || muf->SkipUic) {
+12 -19
View File
@@ -2,7 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */ file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmSourceFile.h" #include "cmSourceFile.h"
#include <array>
#include <utility> #include <utility>
#include "cmGlobalGenerator.h" #include "cmGlobalGenerator.h"
@@ -130,13 +129,11 @@ bool cmSourceFile::FindFullPath(std::string* error)
// Location path // Location path
std::string const& lPath = this->Location.GetFullPath(); std::string const& lPath = this->Location.GetFullPath();
// List of extension lists // List of extension lists
std::array<std::vector<std::string> const*, 2> const extsLists = { std::vector<std::string> exts =
{ &makefile->GetCMakeInstance()->GetSourceExtensions(), makefile->GetCMakeInstance()->GetAllExtensions();
&makefile->GetCMakeInstance()->GetHeaderExtensions() }
};
// Tries to find the file in a given directory // Tries to find the file in a given directory
auto findInDir = [this, &extsLists, &lPath](std::string const& dir) -> bool { auto findInDir = [this, &exts, &lPath](std::string const& dir) -> bool {
// Compute full path // Compute full path
std::string const fullPath = cmSystemTools::CollapseFullPath(lPath, dir); std::string const fullPath = cmSystemTools::CollapseFullPath(lPath, dir);
// Try full path // Try full path
@@ -145,14 +142,12 @@ bool cmSourceFile::FindFullPath(std::string* error)
return true; return true;
} }
// Try full path with extension // Try full path with extension
for (auto& exts : extsLists) { for (std::string const& ext : exts) {
for (std::string const& ext : *exts) { if (!ext.empty()) {
if (!ext.empty()) { std::string extPath = cmStrCat(fullPath, '.', ext);
std::string extPath = cmStrCat(fullPath, '.', ext); if (cmSystemTools::FileExists(extPath)) {
if (cmSystemTools::FileExists(extPath)) { this->FullPath = extPath;
this->FullPath = extPath; return true;
return true;
}
} }
} }
} }
@@ -175,11 +170,9 @@ bool cmSourceFile::FindFullPath(std::string* error)
// Compose error // Compose error
std::string err = std::string err =
cmStrCat("Cannot find source file:\n ", lPath, "\nTried extensions"); cmStrCat("Cannot find source file:\n ", lPath, "\nTried extensions");
for (auto exts : extsLists) { for (std::string const& ext : exts) {
for (std::string const& ext : *exts) { err += " .";
err += " ."; err += ext;
err += ext;
}
} }
if (error != nullptr) { if (error != nullptr) {
*error = std::move(err); *error = std::move(err);
+2 -2
View File
@@ -101,7 +101,7 @@ void cmSourceFileLocation::UpdateExtension(const std::string& name)
cmMakefile const* mf = this->Makefile; cmMakefile const* mf = this->Makefile;
auto cm = mf->GetCMakeInstance(); auto cm = mf->GetCMakeInstance();
if (!gg->GetLanguageFromExtension(ext.c_str()).empty() || if (!gg->GetLanguageFromExtension(ext.c_str()).empty() ||
cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext)) { cm->IsAKnownExtension(ext)) {
// This is a known extension. Use the given filename with extension. // This is a known extension. Use the given filename with extension.
this->Name = cmSystemTools::GetFilenameName(name); this->Name = cmSystemTools::GetFilenameName(name);
this->AmbiguousExtension = false; this->AmbiguousExtension = false;
@@ -157,7 +157,7 @@ bool cmSourceFileLocation::MatchesAmbiguousExtension(
auto ext = cm::string_view(this->Name).substr(loc.Name.size() + 1); auto ext = cm::string_view(this->Name).substr(loc.Name.size() + 1);
cmMakefile const* mf = this->Makefile; cmMakefile const* mf = this->Makefile;
auto cm = mf->GetCMakeInstance(); auto cm = mf->GetCMakeInstance();
return cm->IsSourceExtension(ext) || cm->IsHeaderExtension(ext); return cm->IsAKnownExtension(ext);
} }
bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc) bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
+13 -2
View File
@@ -194,7 +194,7 @@ cmake::cmake(Role role, cmState::Mode mode)
}; };
// The "c" extension MUST precede the "C" extension. // The "c" extension MUST precede the "C" extension.
setupExts(this->SourceFileExtensions, setupExts(this->CLikeSourceFileExtensions,
{ "c", "C", "c++", "cc", "cpp", "cxx", "cu", "m", "M", "mm" }); { "c", "C", "c++", "cc", "cpp", "cxx", "cu", "m", "M", "mm" });
setupExts(this->HeaderFileExtensions, setupExts(this->HeaderFileExtensions,
{ "h", "hh", "h++", "hm", "hpp", "hxx", "in", "txx" }); { "h", "hh", "h++", "hm", "hpp", "hxx", "in", "txx" });
@@ -1959,6 +1959,17 @@ void cmake::AddGlobCacheEntry(bool recurse, bool listDirectories,
backtrace); backtrace);
} }
std::vector<std::string> cmake::GetAllExtensions() const
{
std::vector<std::string> allExt = this->CLikeSourceFileExtensions.ordered;
allExt.insert(allExt.end(), this->HeaderFileExtensions.ordered.begin(),
this->HeaderFileExtensions.ordered.end());
// cuda extensions are also in SourceFileExtensions so we ignore it here
allExt.insert(allExt.end(), this->FortranFileExtensions.ordered.begin(),
this->FortranFileExtensions.ordered.end());
return allExt;
}
std::string cmake::StripExtension(const std::string& file) const std::string cmake::StripExtension(const std::string& file) const
{ {
auto dotpos = file.rfind('.'); auto dotpos = file.rfind('.');
@@ -1968,7 +1979,7 @@ std::string cmake::StripExtension(const std::string& file) const
#else #else
auto ext = cm::string_view(file).substr(dotpos + 1); auto ext = cm::string_view(file).substr(dotpos + 1);
#endif #endif
if (this->IsSourceExtension(ext) || this->IsHeaderExtension(ext)) { if (this->IsAKnownExtension(ext)) {
return file.substr(0, dotpos); return file.substr(0, dotpos);
} }
} }
+15 -26
View File
@@ -264,46 +264,35 @@ public:
this->GeneratorToolsetSet = true; this->GeneratorToolsetSet = true;
} }
const std::vector<std::string>& GetSourceExtensions() const bool IsAKnownSourceExtension(cm::string_view ext) const
{ {
return this->SourceFileExtensions.ordered; return this->CLikeSourceFileExtensions.Test(ext) ||
this->CudaFileExtensions.Test(ext) ||
this->FortranFileExtensions.Test(ext);
} }
bool IsSourceExtension(cm::string_view ext) const bool IsACLikeSourceExtension(cm::string_view ext) const
{ {
return this->SourceFileExtensions.Test(ext); return this->CLikeSourceFileExtensions.Test(ext);
} }
bool IsAKnownExtension(cm::string_view ext) const
{
return this->IsAKnownSourceExtension(ext) || this->IsAHeaderExtension(ext);
}
std::vector<std::string> GetAllExtensions() const;
const std::vector<std::string>& GetHeaderExtensions() const const std::vector<std::string>& GetHeaderExtensions() const
{ {
return this->HeaderFileExtensions.ordered; return this->HeaderFileExtensions.ordered;
} }
bool IsHeaderExtension(cm::string_view ext) const bool IsAHeaderExtension(cm::string_view ext) const
{ {
return this->HeaderFileExtensions.Test(ext); return this->HeaderFileExtensions.Test(ext);
} }
const std::vector<std::string>& GetCudaExtensions() const
{
return this->CudaFileExtensions.ordered;
}
bool IsCudaExtension(cm::string_view ext) const
{
return this->CudaFileExtensions.Test(ext);
}
const std::vector<std::string>& GetFortranExtensions() const
{
return this->FortranFileExtensions.ordered;
}
bool IsFortranExtension(cm::string_view ext) const
{
return this->FortranFileExtensions.Test(ext);
}
// Strips the extension (if present and known) from a filename // Strips the extension (if present and known) from a filename
std::string StripExtension(const std::string& file) const; std::string StripExtension(const std::string& file) const;
@@ -628,7 +617,7 @@ private:
std::string CheckStampList; std::string CheckStampList;
std::string VSSolutionFile; std::string VSSolutionFile;
std::string EnvironmentGenerator; std::string EnvironmentGenerator;
FileExtensions SourceFileExtensions; FileExtensions CLikeSourceFileExtensions;
FileExtensions HeaderFileExtensions; FileExtensions HeaderFileExtensions;
FileExtensions CudaFileExtensions; FileExtensions CudaFileExtensions;
FileExtensions FortranFileExtensions; FileExtensions FortranFileExtensions;
@@ -3,7 +3,7 @@ CMake Error at MissingSource.cmake:1 \(add_library\):
missing.c missing.c
Tried extensions( \.[A-Za-z+]+| Tried extensions \.c \.C .*
)* .*
Call Stack \(most recent call first\): Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\) CMakeLists.txt:3 \(include\)
@@ -3,7 +3,7 @@ CMake Error at global-interface.cmake:2 \(add_library\):
GLOBAL GLOBAL
Tried extensions( \.[A-Za-z+]+| Tried extensions \.c \.C .*
)* .*
Call Stack \(most recent call first\): Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\) CMakeLists.txt:3 \(include\)