Skip to content

Commit ea1c1ed

Browse files
committed
added Path::isHeader2() as a convenience function
1 parent cdf14d9 commit ea1c1ed

6 files changed

Lines changed: 34 additions & 13 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
151151
// Output a warning for the user if he tries to exclude headers
152152
const std::vector<std::string>& ignored = parser.getIgnoredPaths();
153153
const bool warn = std::any_of(ignored.cbegin(), ignored.cend(), [](const std::string& i) {
154-
bool header;
155-
Path::identify(i, &header);
156-
return header;
154+
return Path::isHeader2(i);
157155
});
158156
if (warn) {
159157
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;

gui/resultstree.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,7 @@ void ResultsTree::recheckSelectedFiles()
950950
askFileDir(currentFile);
951951
return;
952952
}
953-
bool header = false;
954-
Path::identify(currentFile.toStdString(), &header);
955-
if (header) {
953+
if (Path::isHeader2(currentFile.toStdString())) {
956954
if (!data[FILE0].toString().isEmpty() && !selectedItems.contains(data[FILE0].toString())) {
957955
selectedItems<<((!mCheckPath.isEmpty() && (data[FILE0].toString().indexOf(mCheckPath) != 0)) ? (mCheckPath + "/" + data[FILE0].toString()) : data[FILE0].toString());
958956
if (!selectedItems.contains(fileNameWithCheckPath))

gui/resultsview.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,8 @@ void ResultsView::updateDetails(const QModelIndex &index)
418418
QString formattedMsg = message;
419419

420420
const QString file0 = data["file0"].toString();
421-
if (!file0.isEmpty()) {
422-
bool header = false;
423-
Path::identify(data["file"].toString().toStdString(), &header);
424-
if (header)
425-
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by")).arg(QDir::toNativeSeparators(file0));
426-
}
421+
if (!file0.isEmpty() && Path::isHeader2(data["file"].toString().toStdString()))
422+
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by")).arg(QDir::toNativeSeparators(file0));
427423

428424
if (data["cwe"].toInt() > 0)
429425
formattedMsg.prepend("CWE: " + QString::number(data["cwe"].toInt()) + "\n");

lib/path.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ Settings::Language Path::identify(const std::string &path, bool *header)
269269
return Settings::Language::None;
270270
}
271271

272+
bool Path::isHeader2(const std::string &path)
273+
{
274+
bool header;
275+
(void)Path::identify(path, &header);
276+
return header;
277+
}
278+
272279
std::string Path::getAbsoluteFilePath(const std::string& filePath)
273280
{
274281
std::string absolute_path;

lib/path.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,17 @@ class CPPCHECKLIB Path {
173173
* @brief Is filename a header based on file extension
174174
* @param path filename to check. path info is optional
175175
* @return true if filename extension is meant for headers
176-
* @deprecated returns only heuristic result - use @identify() instead
176+
* @deprecated returns only heuristic result - use @identify() or @isHeader2() instead
177177
*/
178178
static DEPRECATED bool isHeader(const std::string &path);
179179

180+
/**
181+
* @brief Is filename a header based on file extension
182+
* @param path filename to check. path info is optional
183+
* @return true if filename extension is meant for headers
184+
*/
185+
static bool isHeader2(const std::string &path);
186+
180187
/**
181188
* @brief Identify the language based on the file extension
182189
* @param path filename to check. path info is optional

test/testpath.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestPath : public TestFixture {
4141
TEST_CASE(get_path_from_filename);
4242
TEST_CASE(join);
4343
TEST_CASE(identify);
44+
TEST_CASE(is_header_2);
4445
}
4546

4647
void removeQuotationMarks() const {
@@ -290,6 +291,20 @@ class TestPath : public TestFixture {
290291
ASSERT_EQUALS(Settings::Language::None, Path::identify("index.htm"));
291292
ASSERT_EQUALS(Settings::Language::None, Path::identify("index.html"));
292293
}
294+
295+
void is_header_2() const {
296+
ASSERT(Path::isHeader2("index.h"));
297+
ASSERT(Path::isHeader2("index.hpp"));
298+
ASSERT(Path::isHeader2("index.hxx"));
299+
ASSERT(Path::isHeader2("index.h++"));
300+
ASSERT(Path::isHeader2("index.hh"));
301+
302+
ASSERT(Path::isHeader2("index.c")==false);
303+
ASSERT(Path::isHeader2("index.cpp")==false);
304+
ASSERT(Path::isHeader2("index.header")==false);
305+
ASSERT(Path::isHeader2("index.htm")==false);
306+
ASSERT(Path::isHeader2("index.html")==false);
307+
}
293308
};
294309

295310
REGISTER_TEST(TestPath)

0 commit comments

Comments
 (0)