Skip to content

Commit a8facb7

Browse files
committed
added Path::isHeader2() as a convenience function
1 parent 3e0fd09 commit a8facb7

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
@@ -132,9 +132,7 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
132132
// Output a warning for the user if he tries to exclude headers
133133
const std::vector<std::string>& ignored = parser.getIgnoredPaths();
134134
const bool warn = std::any_of(ignored.cbegin(), ignored.cend(), [](const std::string& i) {
135-
bool header;
136-
Path::identify(i, &header);
137-
return header;
135+
return Path::isHeader2(i);
138136
});
139137
if (warn) {
140138
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
@@ -955,9 +955,7 @@ void ResultsTree::recheckSelectedFiles()
955955
askFileDir(currentFile);
956956
return;
957957
}
958-
bool header = false;
959-
Path::identify(currentFile.toStdString(), &header);
960-
if (header) {
958+
if (Path::isHeader2(currentFile.toStdString())) {
961959
if (!data[FILE0].toString().isEmpty() && !selectedItems.contains(data[FILE0].toString())) {
962960
selectedItems<<((!mCheckPath.isEmpty() && (data[FILE0].toString().indexOf(mCheckPath) != 0)) ? (mCheckPath + "/" + data[FILE0].toString()) : data[FILE0].toString());
963961
if (!selectedItems.contains(fileNameWithCheckPath))

gui/resultsview.cpp

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

401401
const QString file0 = data["file0"].toString();
402-
if (!file0.isEmpty()) {
403-
bool header = false;
404-
Path::identify(data["file"].toString().toStdString(), &header);
405-
if (header)
406-
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by")).arg(QDir::toNativeSeparators(file0));
407-
}
402+
if (!file0.isEmpty() && Path::isHeader2(data["file"].toString().toStdString()))
403+
formattedMsg += QString("\n\n%1: %2").arg(tr("First included by")).arg(QDir::toNativeSeparators(file0));
408404

409405
if (data["cwe"].toInt() > 0)
410406
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
@@ -244,6 +244,13 @@ Settings::Language Path::identify(const std::string &path, bool *header)
244244
return Settings::Language::None;
245245
}
246246

247+
bool Path::isHeader2(const std::string &path)
248+
{
249+
bool header;
250+
(void)Path::identify(path, &header);
251+
return header;
252+
}
253+
247254
std::string Path::getAbsoluteFilePath(const std::string& filePath)
248255
{
249256
std::string absolute_path;

lib/path.h

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

174+
/**
175+
* @brief Is filename a header based on file extension
176+
* @param path filename to check. path info is optional
177+
* @return true if filename extension is meant for headers
178+
*/
179+
static bool isHeader2(const std::string &path);
180+
174181
/**
175182
* @brief Identify the language based on the file extension
176183
* @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
@@ -40,6 +40,7 @@ class TestPath : public TestFixture {
4040
TEST_CASE(get_path_from_filename);
4141
TEST_CASE(join);
4242
TEST_CASE(identify);
43+
TEST_CASE(is_header_2);
4344
}
4445

4546
void removeQuotationMarks() const {
@@ -285,6 +286,20 @@ class TestPath : public TestFixture {
285286
ASSERT_EQUALS(Settings::Language::None, Path::identify("index.htm"));
286287
ASSERT_EQUALS(Settings::Language::None, Path::identify("index.html"));
287288
}
289+
290+
void is_header_2() const {
291+
ASSERT(Path::isHeader2("index.h"));
292+
ASSERT(Path::isHeader2("index.hpp"));
293+
ASSERT(Path::isHeader2("index.hxx"));
294+
ASSERT(Path::isHeader2("index.h++"));
295+
ASSERT(Path::isHeader2("index.hh"));
296+
297+
ASSERT(Path::isHeader2("index.c")==false);
298+
ASSERT(Path::isHeader2("index.cpp")==false);
299+
ASSERT(Path::isHeader2("index.header")==false);
300+
ASSERT(Path::isHeader2("index.htm")==false);
301+
ASSERT(Path::isHeader2("index.html")==false);
302+
}
288303
};
289304

290305
REGISTER_TEST(TestPath)

0 commit comments

Comments
 (0)