Skip to content

Commit 394a472

Browse files
authored
Fix #15096 (clang import: assertion failure for simple hello.c) (#8932)
1 parent 5a3579b commit 394a472

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎lib/tokenlist.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int TokenList::appendFileIfNew(std::string fileName)
102102
if (it != mFiles.cend())
103103
return static_cast<int>(std::distance(mFiles.cbegin(), it));
104104

105-
assert(mTokensFrontBack->front == nullptr); // has no effect if tokens have already been created
105+
assert(mSettings.clang || mTokensFrontBack->front == nullptr); // has no effect if tokens have already been created
106106

107107
// The "mFiles" vector remembers what files have been tokenized..
108108
mFiles.push_back(std::move(fileName));

‎test/cli/clang-import_test.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ def test_log(tmpdir):
128128
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)
129129

130130

131+
def test_include(tmp_path):
132+
test_file = tmp_path / 'test.c'
133+
with open(test_file, 'wt') as f:
134+
f.write('#include "test.h"')
135+
136+
with open(tmp_path / 'test.h', 'wt') as f:
137+
f.write('int x;\n')
138+
139+
exitcode, stdout, stderr = cppcheck(['-q', '--clang', '--debug', str(test_file)])
140+
assert exitcode == 0, stderr if not stdout else stdout
141+
# the number of empty lines after "##file" depends on the builtin declarations in the clang AST dump
142+
assert '##file {}\n'.format(tmp_path / 'test.h') in stdout, stdout
143+
assert '\n1: int x@var1 ;\n' in stdout, stdout
144+
145+
131146
def test_warning(tmpdir): # #12424
132147
test_file = os.path.join(tmpdir, 'test_2')
133148
with open(test_file, 'wt') as f:

‎test/testclangimport.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class TestClangImport : public TestFixture {
116116
TEST_CASE(vardecl5);
117117
TEST_CASE(vardecl6);
118118
TEST_CASE(vardecl7);
119+
TEST_CASE(vardeclInHeader);
119120
TEST_CASE(whileStmt1);
120121
TEST_CASE(whileStmt2);
121122

@@ -1041,6 +1042,15 @@ class TestClangImport : public TestFixture {
10411042
ASSERT_EQUALS("void * * start@1 ;", parse(clang));
10421043
}
10431044

1045+
void vardeclInHeader() {
1046+
// a new file is seen after tokens have been created
1047+
const char clang[] = "|-VarDecl 0x32b8aa0 <1.c:1:1, col:5> col:5 a 'int'\n"
1048+
"`-VarDecl 0x32b8b78 <1.h:1:1, col:5> col:5 b 'int'";
1049+
1050+
ASSERT_EQUALS("int a@1 ; int b@2 ;",
1051+
parse(clang));
1052+
}
1053+
10441054
void whileStmt1() {
10451055
const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n"
10461056
" `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n"

0 commit comments

Comments
 (0)