Skip to content

Commit 40ae723

Browse files
committed
Fix #15096 (clang import: assertion failure for simple hello.c)
1 parent b8e62df commit 40ae723

3 files changed

Lines changed: 25 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ 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+
# the declaration before the #include is needed so the header is seen in the AST dump after tokens have been created
133+
test_file = tmp_path / 'test.c'
134+
with open(test_file, 'wt') as f:
135+
f.write('#include "test.h"')
136+
137+
with open(tmp_path / 'test.h', 'wt') as f:
138+
f.write('int x;\n')
139+
140+
exitcode, stdout, stderr = cppcheck(['-q', '--clang', '--debug', str(test_file)])
141+
assert exitcode == 0, stderr if not stdout else stdout
142+
assert '##file {}\n\n1: int x@var2 ;\n'.format(tmp_path / 'test.h') in stdout, stdout
143+
144+
131145
def test_warning(tmpdir): # #12424
132146
test_file = os.path.join(tmpdir, 'test_2')
133147
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

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

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

0 commit comments

Comments
 (0)