diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index a76493139c7..26b24d649c8 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -102,7 +102,7 @@ int TokenList::appendFileIfNew(std::string fileName) if (it != mFiles.cend()) return static_cast(std::distance(mFiles.cbegin(), it)); - assert(mTokensFrontBack->front == nullptr); // has no effect if tokens have already been created + assert(mSettings.clang || mTokensFrontBack->front == nullptr); // has no effect if tokens have already been created // The "mFiles" vector remembers what files have been tokenized.. mFiles.push_back(std::move(fileName)); diff --git a/test/cli/clang-import_test.py b/test/cli/clang-import_test.py index e4ec19a5ada..da5bb2f3af7 100644 --- a/test/cli/clang-import_test.py +++ b/test/cli/clang-import_test.py @@ -128,6 +128,21 @@ def test_log(tmpdir): assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines) +def test_include(tmp_path): + test_file = tmp_path / 'test.c' + with open(test_file, 'wt') as f: + f.write('#include "test.h"') + + with open(tmp_path / 'test.h', 'wt') as f: + f.write('int x;\n') + + exitcode, stdout, stderr = cppcheck(['-q', '--clang', '--debug', str(test_file)]) + assert exitcode == 0, stderr if not stdout else stdout + # the number of empty lines after "##file" depends on the builtin declarations in the clang AST dump + assert '##file {}\n'.format(tmp_path / 'test.h') in stdout, stdout + assert '\n1: int x@var1 ;\n' in stdout, stdout + + def test_warning(tmpdir): # #12424 test_file = os.path.join(tmpdir, 'test_2') with open(test_file, 'wt') as f: diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index 229e4c3c982..d307cf201de 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -116,6 +116,7 @@ class TestClangImport : public TestFixture { TEST_CASE(vardecl5); TEST_CASE(vardecl6); TEST_CASE(vardecl7); + TEST_CASE(vardeclInHeader); TEST_CASE(whileStmt1); TEST_CASE(whileStmt2); @@ -1040,6 +1041,15 @@ class TestClangImport : public TestFixture { ASSERT_EQUALS("void * * start@1 ;", parse(clang)); } + void vardeclInHeader() { + // a new file is seen after tokens have been created + const char clang[] = "|-VarDecl 0x32b8aa0 <1.c:1:1, col:5> col:5 a 'int'\n" + "`-VarDecl 0x32b8b78 <1.h:1:1, col:5> col:5 b 'int'"; + + ASSERT_EQUALS("int a@1 ; int b@2 ;", + parse(clang)); + } + void whileStmt1() { const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n" " `-CompoundStmt 0x3d45c48 \n"