Skip to content

Commit bb78bd8

Browse files
authored
refs #14265: some simplecpp errors might lack column information (danmar#7965)
1 parent 2ad4620 commit bb78bd8

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

lib/preprocessor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
870870
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
871871
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
872872
if (pos1 < pos2 && pos2 != std::string::npos)
873-
missingInclude(out.location.file(), out.location.line, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
873+
missingInclude(out.location.file(), out.location.line, out.location.col, out.msg.substr(pos1+1, pos2-pos1-1), out.msg[pos1] == '\"' ? UserHeader : SystemHeader);
874874
}
875875
break;
876876
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
@@ -910,14 +910,14 @@ void Preprocessor::error(const std::string &filename, unsigned int linenr, const
910910
}
911911

912912
// Report that include is missing
913-
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType)
913+
void Preprocessor::missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType)
914914
{
915915
if (!mSettings.checks.isEnabled(Checks::missingInclude))
916916
return;
917917

918918
std::list<ErrorMessage::FileLocation> locationList;
919919
if (!filename.empty()) {
920-
locationList.emplace_back(filename, linenr, 0);
920+
locationList.emplace_back(filename, linenr, col);
921921
}
922922
ErrorMessage errmsg(std::move(locationList), mFile0, Severity::information,
923923
(headerType==SystemHeader) ?
@@ -933,8 +933,8 @@ void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &se
933933
std::vector<std::string> files;
934934
simplecpp::TokenList tokens(files);
935935
Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::CPP);
936-
preprocessor.missingInclude("", 1, "", UserHeader);
937-
preprocessor.missingInclude("", 1, "", SystemHeader);
936+
preprocessor.missingInclude("", 1, 2, "", UserHeader);
937+
preprocessor.missingInclude("", 1, 2, "", SystemHeader);
938938
preprocessor.error("", 1, "#error message"); // #error ..
939939
}
940940

lib/preprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
158158
SystemHeader
159159
};
160160

161-
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
161+
void missingInclude(const std::string &filename, unsigned int linenr, unsigned int col, const std::string &header, HeaderTypes headerType);
162162
void error(const std::string &filename, unsigned int linenr, const std::string &msg);
163163

164164
void addRemarkComments(const simplecpp::TokenList &tokens, std::vector<RemarkComment> &remarkComments) const;

test/cli/helloworld_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def test_missing_include_system(): # #11283
357357
]
358358

359359
_, _, stderr = cppcheck(args, cwd=__script_dir)
360-
assert stderr.replace('\\', '/') == 'helloworld/main.c:1:0: information: Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n'
360+
assert stderr.replace('\\', '/') == 'helloworld/main.c:1:2: information: Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n'
361361

362362

363363
def test_sarif():

test/cli/other_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def test_missing_include(tmpdir): # #11283
2525
test_file = os.path.join(tmpdir, 'test.c')
2626
with open(test_file, 'wt') as f:
2727
f.write("""
28-
#include "test.h"
29-
""")
28+
#include "test.h"
29+
""")
3030

3131
args = ['--enable=missingInclude', '--template=simple', test_file]
3232

3333
_, _, stderr = cppcheck(args)
34-
assert stderr == '{}:2:0: information: Include file: "test.h" not found. [missingInclude]\n'.format(test_file)
34+
assert stderr == '{}:2:2: information: Include file: "test.h" not found. [missingInclude]\n'.format(test_file)
3535

3636

3737
def __test_missing_include_check_config(tmpdir, use_j):

test/testpreprocessor.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,7 @@ class TestPreprocessor : public TestFixture {
24242424
const char code[] = "#include \"header.h\"";
24252425
(void)getcodeforcfg(settings, *this, code, "", "test.c");
24262426

2427-
ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str());
2427+
ASSERT_EQUALS("test.c:1:2: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str());
24282428
}
24292429

24302430
// test for missing local include - no include path given
@@ -2440,7 +2440,7 @@ class TestPreprocessor : public TestFixture {
24402440
const char code[] = "#include \"header.h\"";
24412441
(void)getcodeforcfg(settings, *this, code, "", "test.c");
24422442

2443-
ASSERT_EQUALS("test.c:1:0: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str());
2443+
ASSERT_EQUALS("test.c:1:2: information: Include file: \"header.h\" not found. [missingInclude]\n", errout_str());
24442444
}
24452445

24462446
// test for existing local include - include path provided
@@ -2490,7 +2490,7 @@ class TestPreprocessor : public TestFixture {
24902490
std::string code("#include \"" + header + "\"");
24912491
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
24922492

2493-
ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str());
2493+
ASSERT_EQUALS("test.c:1:2: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str());
24942494
}
24952495

24962496
// test for missing system include - system includes are not searched for in relative path
@@ -2506,7 +2506,7 @@ class TestPreprocessor : public TestFixture {
25062506
const char code[] = "#include <header.h>";
25072507
(void)getcodeforcfg(settings, *this, code, "", "test.c");
25082508

2509-
ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
2509+
ASSERT_EQUALS("test.c:1:2: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
25102510
}
25112511

25122512
// test for missing system include
@@ -2520,7 +2520,7 @@ class TestPreprocessor : public TestFixture {
25202520
const char code[] = "#include <header.h>";
25212521
(void)getcodeforcfg(settings, *this, code, "", "test.c");
25222522

2523-
ASSERT_EQUALS("test.c:1:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
2523+
ASSERT_EQUALS("test.c:1:2: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
25242524
}
25252525

25262526
// test for existing system include in system include path
@@ -2570,7 +2570,7 @@ class TestPreprocessor : public TestFixture {
25702570
std::string code("#include <" + header + ">");
25712571
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
25722572

2573-
ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
2573+
ASSERT_EQUALS("test.c:1:2: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
25742574
}
25752575

25762576
// test for missing local and system include
@@ -2590,9 +2590,9 @@ class TestPreprocessor : public TestFixture {
25902590
"#include \"header2.h\"";
25912591
(void)getcodeforcfg(settings, *this, code, "", "test.c");
25922592

2593-
ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n"
2594-
"test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2595-
"test.c:3:0: information: Include file: <missing2.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
2593+
ASSERT_EQUALS("test.c:1:2: information: Include file: \"missing.h\" not found. [missingInclude]\n"
2594+
"test.c:2:2: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2595+
"test.c:3:2: information: Include file: <missing2.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
25962596
}
25972597

25982598
void testMissingIncludeCheckConfig() {
@@ -2626,12 +2626,12 @@ class TestPreprocessor : public TestFixture {
26262626
"#include <" + missing4 + ">\n");
26272627
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
26282628

2629-
ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n"
2630-
"test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2631-
"test.c:3:0: information: Include file: <missing2.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2632-
"test.c:6:0: information: Include file: \"header4.h\" not found. [missingInclude]\n"
2633-
"test.c:9:0: information: Include file: \"" + missing3 + "\" not found. [missingInclude]\n"
2634-
"test.c:11:0: information: Include file: <" + missing4 + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
2629+
ASSERT_EQUALS("test.c:1:2: information: Include file: \"missing.h\" not found. [missingInclude]\n"
2630+
"test.c:2:2: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2631+
"test.c:3:2: information: Include file: <missing2.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"
2632+
"test.c:6:2: information: Include file: \"header4.h\" not found. [missingInclude]\n"
2633+
"test.c:9:2: information: Include file: \"" + missing3 + "\" not found. [missingInclude]\n"
2634+
"test.c:11:2: information: Include file: <" + missing4 + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
26352635
}
26362636

26372637
void hasInclude() {

0 commit comments

Comments
 (0)