Skip to content

Commit 184d2d4

Browse files
committed
added Path::isHeader2() as a convenience function
1 parent d50c451 commit 184d2d4

6 files changed

Lines changed: 34 additions & 13 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
185185
// Output a warning for the user if he tries to exclude headers
186186
const std::vector<std::string>& ignored = getIgnoredPaths();
187187
const bool warn = std::any_of(ignored.cbegin(), ignored.cend(), [](const std::string& i) {
188-
bool header;
189-
Path::identify(i, &header);
190-
return header;
188+
return Path::isHeader2(i);
191189
});
192190
if (warn) {
193191
mLogger.printMessage("filename exclusion does not apply to header (.h and .hpp) files.");

gui/resultstree.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,7 @@ void ResultsTree::recheckSelectedFiles()
962962
askFileDir(currentFile);
963963
return;
964964
}
965-
bool header = false;
966-
Path::identify(currentFile.toStdString(), &header);
967-
if (header) {
965+
if (Path::isHeader2(currentFile.toStdString())) {
968966
if (!data[FILE0].toString().isEmpty() && !selectedItems.contains(data[FILE0].toString())) {
969967
selectedItems<<((!mCheckPath.isEmpty() && (data[FILE0].toString().indexOf(mCheckPath) != 0)) ? (mCheckPath + "/" + data[FILE0].toString()) : data[FILE0].toString());
970968
if (!selectedItems.contains(fileNameWithCheckPath))

gui/resultsview.cpp

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

464464
const QString file0 = data["file0"].toString();
465-
if (!file0.isEmpty()) {
466-
bool header = false;
467-
Path::identify(data["file"].toString().toStdString(), &header);
468-
if (header)
469-
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by"), QDir::toNativeSeparators(file0));
470-
}
465+
if (!file0.isEmpty() && Path::isHeader2(data["file"].toString().toStdString()))
466+
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by"), QDir::toNativeSeparators(file0));
471467

472468
if (data["cwe"].toInt() > 0)
473469
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
@@ -270,6 +270,13 @@ Standards::Language Path::identify(const std::string &path, bool *header)
270270
return Standards::Language::None;
271271
}
272272

273+
bool Path::isHeader2(const std::string &path)
274+
{
275+
bool header;
276+
(void)Path::identify(path, &header);
277+
return header;
278+
}
279+
273280
std::string Path::getAbsoluteFilePath(const std::string& filePath)
274281
{
275282
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
@@ -46,6 +46,7 @@ class TestPath : public TestFixture {
4646
TEST_CASE(sameFileName);
4747
TEST_CASE(getFilenameExtension);
4848
TEST_CASE(identify);
49+
TEST_CASE(is_header_2);
4950
}
5051

5152
void removeQuotationMarks() const {
@@ -361,6 +362,20 @@ class TestPath : public TestFixture {
361362
ASSERT_EQUALS(Standards::Language::None, Path::identify("index.htm"));
362363
ASSERT_EQUALS(Standards::Language::None, Path::identify("index.html"));
363364
}
365+
366+
void is_header_2() const {
367+
ASSERT(Path::isHeader2("index.h"));
368+
ASSERT(Path::isHeader2("index.hpp"));
369+
ASSERT(Path::isHeader2("index.hxx"));
370+
ASSERT(Path::isHeader2("index.h++"));
371+
ASSERT(Path::isHeader2("index.hh"));
372+
373+
ASSERT(Path::isHeader2("index.c")==false);
374+
ASSERT(Path::isHeader2("index.cpp")==false);
375+
ASSERT(Path::isHeader2("index.header")==false);
376+
ASSERT(Path::isHeader2("index.htm")==false);
377+
ASSERT(Path::isHeader2("index.html")==false);
378+
}
364379
};
365380

366381
REGISTER_TEST(TestPath)

0 commit comments

Comments
 (0)