Skip to content

Commit

Permalink
Path: removed deprecated functions / renamed isHeader2() to `isHead…
Browse files Browse the repository at this point in the history
…er()` (#6427)
  • Loading branch information
firewave authored May 23, 2024
1 parent ab69847 commit b17a347
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 153 deletions.
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
// Output a warning for the user if he tries to exclude headers
const std::vector<std::string>& ignored = getIgnoredPaths();
const bool warn = std::any_of(ignored.cbegin(), ignored.cend(), [](const std::string& i) {
return Path::isHeader2(i);
return Path::isHeader(i);
});
if (warn) {
mLogger.printMessage("filename exclusion does not apply to header (.h and .hpp) files.");
Expand Down
2 changes: 1 addition & 1 deletion gui/resultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ void ResultsTree::recheckSelectedFiles()
askFileDir(currentFile);
return;
}
if (Path::isHeader2(currentFile.toStdString())) {
if (Path::isHeader(currentFile.toStdString())) {
if (!data[FILE0].toString().isEmpty() && !selectedItems.contains(data[FILE0].toString())) {
selectedItems<<((!mCheckPath.isEmpty() && (data[FILE0].toString().indexOf(mCheckPath) != 0)) ? (mCheckPath + "/" + data[FILE0].toString()) : data[FILE0].toString());
if (!selectedItems.contains(fileNameWithCheckPath))
Expand Down
2 changes: 1 addition & 1 deletion gui/resultsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ void ResultsView::updateDetails(const QModelIndex &index)
QString formattedMsg = message;

const QString file0 = data["file0"].toString();
if (!file0.isEmpty() && Path::isHeader2(data["file"].toString().toStdString()))
if (!file0.isEmpty() && Path::isHeader(data["file"].toString().toStdString()))
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by")).arg(QDir::toNativeSeparators(file0));

if (data["cwe"].toInt() > 0)
Expand Down
34 changes: 1 addition & 33 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,44 +207,12 @@ static const std::unordered_set<std::string> header_exts = {
".h", ".hpp", ".h++", ".hxx", ".hh"
};

bool Path::isC(const std::string &path)
{
// In unix, ".C" is considered C++ file
const std::string extension = getFilenameExtension(path);
return extension == ".c" ||
extension == ".cl";
}

bool Path::isCPP(const std::string &path)
{
const std::string extension = getFilenameExtensionInLowerCase(path);
return extension == ".cpp" ||
extension == ".cxx" ||
extension == ".cc" ||
extension == ".c++" ||
extension == ".hpp" ||
extension == ".hxx" ||
extension == ".hh" ||
extension == ".tpp" ||
extension == ".txx" ||
extension == ".ipp" ||
extension == ".ixx" ||
getFilenameExtension(path) == ".C"; // In unix, ".C" is considered C++ file
}

bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
{
bool header = false;
return (identify(path, &header) != Standards::Language::None && !header) || extra.find(getFilenameExtension(path)) != extra.end();
}

// cppcheck-suppress unusedFunction
bool Path::isHeader(const std::string &path)
{
const std::string extension = getFilenameExtensionInLowerCase(path);
return startsWith(extension, ".h");
}

Standards::Language Path::identify(const std::string &path, bool *header)
{
// cppcheck-suppress uninitvar - TODO: FP
Expand Down Expand Up @@ -274,7 +242,7 @@ Standards::Language Path::identify(const std::string &path, bool *header)
return Standards::Language::None;
}

bool Path::isHeader2(const std::string &path)
bool Path::isHeader(const std::string &path)
{
bool header;
(void)Path::identify(path, &header);
Expand Down
26 changes: 1 addition & 25 deletions lib/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,36 +153,12 @@ class CPPCHECKLIB Path {
*/
static bool acceptFile(const std::string &path, const std::set<std::string> &extra);

/**
* @brief Identify language based on file extension.
* @param path filename to check. path info is optional
* @return true if extension is meant for C files
* @deprecated does not account for headers - use @identify() instead
*/
static DEPRECATED bool isC(const std::string &path);

/**
* @brief Identify language based on file extension.
* @param path filename to check. path info is optional
* @return true if extension is meant for C++ files
* @deprecated returns true for some header extensions - use @identify() instead
*/
static DEPRECATED bool isCPP(const std::string &path);

/**
* @brief Is filename a header based on file extension
* @param path filename to check. path info is optional
* @return true if filename extension is meant for headers
* @deprecated returns only heuristic result - use @identify() or @isHeader2() instead
*/
static DEPRECATED bool isHeader(const std::string &path);

/**
* @brief Is filename a header based on file extension
* @param path filename to check. path info is optional
* @return true if filename extension is meant for headers
*/
static bool isHeader2(const std::string &path);
static bool isHeader(const std::string &path);

/**
* @brief Identify the language based on the file extension
Expand Down
105 changes: 13 additions & 92 deletions test/testpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ class TestPath : public TestFixture {
TEST_CASE(getCurrentExecutablePath);
TEST_CASE(isAbsolute);
TEST_CASE(getRelative);
TEST_CASE(is_c);
TEST_CASE(is_cpp);
TEST_CASE(is_header);
TEST_CASE(get_path_from_filename);
TEST_CASE(join);
TEST_CASE(isDirectory);
TEST_CASE(isFile);
TEST_CASE(sameFileName);
TEST_CASE(getFilenameExtension);
TEST_CASE(identify);
TEST_CASE(is_header_2);
TEST_CASE(is_header);
}

void removeQuotationMarks() const {
Expand Down Expand Up @@ -131,82 +128,6 @@ class TestPath : public TestFixture {
ASSERT_EQUALS("C:/foobar/test.cpp", Path::getRelativePath("C:/foobar/test.cpp", basePaths));
}

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif

void is_c() const {
ASSERT(Path::isC("index.c"));
ASSERT(Path::isC("index.cl"));
ASSERT(Path::isC("C:\\foo\\index.c"));
ASSERT(Path::isC("/mnt/c/foo/index.c"));

// In unix .C is considered C++
#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
ASSERT_EQUALS(true, Path::isC("C:\\foo\\index.C"));
#else
ASSERT_EQUALS(false, Path::isC("C:\\foo\\index.C"));
#endif

ASSERT(Path::isC("index.cpp")==false);
ASSERT(Path::isC("")==false);
ASSERT(Path::isC("c")==false);

// unlike isCPP() it does not account for headers
ASSERT(Path::isC(".h")==false);
}

void is_cpp() const {
ASSERT(Path::isCPP("index.cpp"));
ASSERT(Path::isCPP("index.cxx"));
ASSERT(Path::isCPP("index.cc"));
ASSERT(Path::isCPP("index.c++"));
ASSERT(Path::isCPP("index.tpp"));
ASSERT(Path::isCPP("index.txx"));
ASSERT(Path::isCPP("index.ipp"));
ASSERT(Path::isCPP("index.ixx"));
ASSERT(Path::isCPP("C:\\foo\\index.cpp"));
ASSERT(Path::isCPP("C:\\foo\\index.Cpp"));
ASSERT(Path::isCPP("/mnt/c/foo/index.cpp"));
ASSERT(Path::isCPP("/mnt/c/foo/index.Cpp"));

// In unix .C is considered C++
#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
ASSERT_EQUALS(false, Path::isCPP("index.C"));
#else
ASSERT_EQUALS(true, Path::isCPP("index.C"));
#endif

ASSERT(Path::isCPP("index.c")==false);

// C++ headers are also considered C++
ASSERT(Path::isCPP("index.hpp"));
// .h++ is missing in the list of C++ headers
ASSERT(Path::isCPP("index.h++")==false);
}

void is_header() const {
ASSERT(Path::isHeader("index.h"));
ASSERT(Path::isHeader("index.hpp"));
ASSERT(Path::isHeader("index.hxx"));
ASSERT(Path::isHeader("index.h++"));
ASSERT(Path::isHeader("index.hh"));

ASSERT(Path::isHeader("index.c")==false);
ASSERT(Path::isHeader("index.cpp")==false);

// function uses heuristic approach which causes these false positives
// no need to fix - function is deprecated and was replaced by identify()
TODO_ASSERT(Path::isHeader("index.header")==false);
TODO_ASSERT(Path::isHeader("index.htm")==false);
TODO_ASSERT(Path::isHeader("index.html")==false);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif

void get_path_from_filename() const {
ASSERT_EQUALS("", Path::getPathFromFilename("index.h"));
ASSERT_EQUALS("/tmp/", Path::getPathFromFilename("/tmp/index.h"));
Expand Down Expand Up @@ -368,18 +289,18 @@ class TestPath : public TestFixture {
ASSERT_EQUALS(Standards::Language::None, Path::identify("index.html"));
}

void is_header_2() const {
ASSERT(Path::isHeader2("index.h"));
ASSERT(Path::isHeader2("index.hpp"));
ASSERT(Path::isHeader2("index.hxx"));
ASSERT(Path::isHeader2("index.h++"));
ASSERT(Path::isHeader2("index.hh"));

ASSERT(Path::isHeader2("index.c")==false);
ASSERT(Path::isHeader2("index.cpp")==false);
ASSERT(Path::isHeader2("index.header")==false);
ASSERT(Path::isHeader2("index.htm")==false);
ASSERT(Path::isHeader2("index.html")==false);
void is_header() const {
ASSERT(Path::isHeader("index.h"));
ASSERT(Path::isHeader("index.hpp"));
ASSERT(Path::isHeader("index.hxx"));
ASSERT(Path::isHeader("index.h++"));
ASSERT(Path::isHeader("index.hh"));

ASSERT(Path::isHeader("index.c")==false);
ASSERT(Path::isHeader("index.cpp")==false);
ASSERT(Path::isHeader("index.header")==false);
ASSERT(Path::isHeader("index.htm")==false);
ASSERT(Path::isHeader("index.html")==false);
}
};

Expand Down

0 comments on commit b17a347

Please sign in to comment.