Skip to content

Commit f072ace

Browse files
committed
removed support for #file/#endfile [skip ci]
1 parent db9f970 commit f072ace

File tree

5 files changed

+86
-76
lines changed

5 files changed

+86
-76
lines changed

lib/cppcheck.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,15 +1132,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11321132
codeWithoutCfg = preprocessor.getcode(currentConfig, files, true);
11331133
});
11341134

1135-
if (startsWith(codeWithoutCfg,"#file"))
1136-
codeWithoutCfg.insert(0U, "//");
11371135
std::string::size_type pos = 0;
1138-
while ((pos = codeWithoutCfg.find("\n#file",pos)) != std::string::npos)
1139-
codeWithoutCfg.insert(pos+1U, "//");
1140-
pos = 0;
1141-
while ((pos = codeWithoutCfg.find("\n#endfile",pos)) != std::string::npos)
1142-
codeWithoutCfg.insert(pos+1U, "//");
1143-
pos = 0;
11441136
while ((pos = codeWithoutCfg.find(Preprocessor::macroChar,pos)) != std::string::npos)
11451137
codeWithoutCfg[pos] = ' ';
11461138
mErrorLogger.reportOut(codeWithoutCfg, Color::Reset);

test/cli/other_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4166,6 +4166,43 @@ def test_no_valid_configuration(tmp_path):
41664166
]
41674167

41684168

4169+
# The implementation for "A::a" is missing - so don't check if "A::b" is used or not
4170+
def test_unused_private_function_incomplete_impl(tmpdir):
4171+
test_inc = os.path.join(tmpdir, 'test.h')
4172+
with open(test_inc, 'wt') as f:
4173+
f.write(
4174+
"""
4175+
class A
4176+
{
4177+
public:
4178+
A();
4179+
void a();
4180+
private:
4181+
void b();
4182+
};
4183+
""")
4184+
4185+
test_file = os.path.join(tmpdir, 'test.cpp')
4186+
with open(test_file, 'wt') as f:
4187+
f.write(
4188+
"""
4189+
#include "test.h"
4190+
4191+
A::A() { }
4192+
void A::b() { }
4193+
""")
4194+
4195+
args = [
4196+
'-q',
4197+
'--template=simple',
4198+
test_file
4199+
]
4200+
4201+
ret, stdout, stderr = cppcheck(args)
4202+
assert stdout == ''
4203+
assert stderr.splitlines() == []
4204+
assert ret == 0, stdout
4205+
41694206
def test_no_valid_configuration_check_config(tmp_path):
41704207
test_file = tmp_path / 'test.c'
41714208
with open(test_file, "w") as f:

test/testpreprocessor.cpp

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class TestPreprocessor : public TestFixture {
140140
cfgs = preprocessor.getConfigs();
141141
for (const std::string & config : cfgs) {
142142
try {
143-
const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr);
143+
const bool writeLocations = (strstr(code, "#include") != nullptr);
144144
cfgcode[config] = preprocessor.getcode(config, files, writeLocations);
145145
} catch (const simplecpp::Output &) {
146146
cfgcode[config] = "";
@@ -390,7 +390,7 @@ class TestPreprocessor : public TestFixture {
390390
std::vector<std::string> files;
391391
simplecpp::OutputList outputList;
392392
simplecpp::TokenList tokens(code,files,"test.c",&outputList);
393-
Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C); // TODO: do we need to consider #file?
393+
Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C);
394394
ASSERT(preprocessor.loadFiles(files));
395395
ASSERT(!preprocessor.reportOutput(outputList, true));
396396
preprocessor.removeComments();
@@ -405,7 +405,7 @@ class TestPreprocessor : public TestFixture {
405405
std::size_t getHash(const char (&code)[size]) {
406406
std::vector<std::string> files;
407407
simplecpp::TokenList tokens(code,files,"test.c");
408-
Preprocessor preprocessor(tokens, settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file?
408+
Preprocessor preprocessor(tokens, settingsDefault, *this, Standards::Language::C);
409409
ASSERT(preprocessor.loadFiles(files));
410410
preprocessor.removeComments();
411411
return preprocessor.calculateHash("");
@@ -470,16 +470,19 @@ class TestPreprocessor : public TestFixture {
470470
void error4() {
471471
// In included file
472472
{
473+
ScopedFile header("ab.h", "#error hello world!\n");
473474
const auto settings = dinit(Settings, $.userDefines = "TEST");
474-
const char code[] = "#file \"ab.h\"\n#error hello world!\n#endfile";
475+
const char code[] = "#include \"ab.h\"";
475476
(void)getcodeforcfg(settings, *this, code, "TEST", "test.c");
476477
ASSERT_EQUALS("[ab.h:1:2]: (error) #error hello world! [preprocessorErrorDirective]\n", errout_str());
477478
}
478479

479480
// After including a file
480481
{
482+
ScopedFile header("ab.h", "");
481483
const auto settings = dinit(Settings, $.userDefines = "TEST");
482-
const char code[] = "#file \"ab.h\"\n\n#endfile\n#error aaa";
484+
const char code[] = "#include \"ab.h\"\n"
485+
"#error aaa";
483486
(void)getcodeforcfg(settings, *this, code, "TEST", "test.c");
484487
ASSERT_EQUALS("[test.c:2:2]: (error) #error aaa [preprocessorErrorDirective]\n", errout_str());
485488
}
@@ -582,35 +585,35 @@ class TestPreprocessor : public TestFixture {
582585
}
583586

584587
void includeguard1() {
588+
ScopedFile header("abc.h",
589+
"#ifndef abcH\n"
590+
"#define abcH\n"
591+
"#endif\n");
585592
// Handling include guards..
586-
const char filedata[] = "#file \"abc.h\"\n"
587-
"#ifndef abcH\n"
588-
"#define abcH\n"
589-
"#endif\n"
590-
"#endfile\n"
593+
const char filedata[] = "#include \"abc.h\"\n"
591594
"#ifdef ABC\n"
592595
"#endif";
593596
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
594597
}
595598

596599
void includeguard2() {
600+
ScopedFile header("abc.h",
601+
"foo\n"
602+
"#ifdef ABC\n"
603+
"\n"
604+
"#endif\n");
597605
// Handling include guards..
598-
const char filedata[] = "#file \"abc.h\"\n"
599-
"foo\n"
600-
"#ifdef ABC\n"
601-
"\n"
602-
"#endif\n"
603-
"#endfile\n";
606+
const char filedata[] = "#include \"abc.h\"\n";
604607
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
605608
}
606609

607610

608611
void ifdefwithfile() {
612+
ScopedFile header("abc.h", "class A{};/*\n\n\n\n\n\n\n*/\n");
613+
609614
// Handling include guards..
610615
const char filedata[] = "#ifdef ABC\n"
611-
"#file \"abc.h\"\n"
612-
"class A{};/*\n\n\n\n\n\n\n*/\n"
613-
"#endfile\n"
616+
"#include \"abc.h\"\n"
614617
"#endif\n"
615618
"int main() {}\n";
616619

@@ -1574,10 +1577,10 @@ class TestPreprocessor : public TestFixture {
15741577
}
15751578

15761579
{
1577-
const char filedata[] = "#file \"abc.h\"\n"
1578-
"#define a\n"
1579-
"\"\n"
1580-
"#endfile\n";
1580+
ScopedFile header("abc.h",
1581+
"#define a\n"
1582+
"\"\n");
1583+
const char filedata[] = "#include \"abc.h\"";
15811584

15821585
// expand macros..
15831586
const std::string actual(expandMacros(filedata, *this));
@@ -1587,9 +1590,9 @@ class TestPreprocessor : public TestFixture {
15871590
}
15881591

15891592
{
1590-
const char filedata[] = "#file \"abc.h\"\n"
1591-
"#define a\n"
1592-
"#endfile\n"
1593+
ScopedFile header("abc.h",
1594+
"#define a\n");
1595+
const char filedata[] = "#include \"abc.h\"\n"
15931596
"\"\n";
15941597

15951598
// expand macros..
@@ -2284,14 +2287,14 @@ class TestPreprocessor : public TestFixture {
22842287
}
22852288

22862289
void getConfigs7e() {
2290+
ScopedFile header("test.h",
2291+
"#ifndef test_h\n"
2292+
"#define test_h\n"
2293+
"#ifdef ABC\n"
2294+
"#endif\n"
2295+
"#endif\n");
22872296
const char filedata[] = "#ifdef ABC\n"
2288-
"#file \"test.h\"\n"
2289-
"#ifndef test_h\n"
2290-
"#define test_h\n"
2291-
"#ifdef ABC\n"
2292-
"#endif\n"
2293-
"#endif\n"
2294-
"#endfile\n"
2297+
"#include \"test.h\"\n"
22952298
"#endif\n";
22962299
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
22972300
}
@@ -2313,12 +2316,12 @@ class TestPreprocessor : public TestFixture {
23132316
}
23142317

23152318
void getConfigs11() { // #9832 - include guards
2316-
const char filedata[] = "#file \"test.h\"\n"
2317-
"#if !defined(test_h)\n"
2318-
"#define test_h\n"
2319-
"123\n"
2320-
"#endif\n"
2321-
"#endfile\n";
2319+
ScopedFile header("test.h",
2320+
"#if !defined(test_h)\n"
2321+
"#define test_h\n"
2322+
"123\n"
2323+
"#endif\n");
2324+
const char filedata[] = "#include \"test.h\"\n";
23222325
ASSERT_EQUALS("\n", getConfigsStr(filedata));
23232326
}
23242327

test/testtokenize.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8746,14 +8746,11 @@ class TestTokenizer : public TestFixture {
87468746
}
87478747

87488748
void testDirectiveIncludeLocations() {
8749+
ScopedFile inc1("inc1.h", "#define macro2 val\n#include \"inc2.h\"\n#define macro4 val\n");
8750+
ScopedFile inc2("inc2.h", "#define macro3 val\n");
8751+
// TODO: preprocess
87498752
const char filedata[] = "#define macro1 val\n"
8750-
"#file \"inc1.h\"\n"
8751-
"#define macro2 val\n"
8752-
"#file \"inc2.h\"\n"
8753-
"#define macro3 val\n"
8754-
"#endfile\n"
8755-
"#define macro4 val\n"
8756-
"#endfile\n"
8753+
"#include \"inc1.h\"\n"
87578754
"#define macro5 val\n";
87588755
const char dumpdata[] = " <directivelist>\n"
87598756
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro1 val\">\n"
@@ -8764,7 +8761,7 @@ class TestTokenizer : public TestFixture {
87648761
" </directive>\n"
87658762
" <directive file=\"test.c\" linenr=\"2\" str=\"#include &quot;inc1.h&quot;\">\n"
87668763
" <token column=\"1\" str=\"#\"/>\n"
8767-
" <token column=\"2\" str=\"file\"/>\n"
8764+
" <token column=\"2\" str=\"include\"/>\n"
87688765
" <token column=\"7\" str=\"&quot;inc1.h&quot;\"/>\n"
87698766
" </directive>\n"
87708767
" <directive file=\"inc1.h\" linenr=\"1\" str=\"#define macro2 val\">\n"
@@ -8775,7 +8772,7 @@ class TestTokenizer : public TestFixture {
87758772
" </directive>\n"
87768773
" <directive file=\"inc1.h\" linenr=\"2\" str=\"#include &quot;inc2.h&quot;\">\n"
87778774
" <token column=\"1\" str=\"#\"/>\n"
8778-
" <token column=\"2\" str=\"file\"/>\n"
8775+
" <token column=\"2\" str=\"include\"/>\n"
87798776
" <token column=\"7\" str=\"&quot;inc2.h&quot;\"/>\n"
87808777
" </directive>\n"
87818778
" <directive file=\"inc2.h\" linenr=\"1\" str=\"#define macro3 val\">\n"

test/testunusedprivfunc.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class TestUnusedPrivateFunction : public TestFixture {
5858

5959
TEST_CASE(classInClass);
6060
TEST_CASE(sameFunctionNames);
61-
TEST_CASE(incompleteImplementation);
6261

6362
TEST_CASE(derivedClass); // skip warning for derived classes. It might be a virtual function.
6463

@@ -482,24 +481,6 @@ class TestUnusedPrivateFunction : public TestFixture {
482481
ASSERT_EQUALS("", errout_str());
483482
}
484483

485-
void incompleteImplementation() {
486-
// The implementation for "A::a" is missing - so don't check if
487-
// "A::b" is used or not
488-
check("#file \"test.h\"\n"
489-
"class A\n"
490-
"{\n"
491-
"public:\n"
492-
" A();\n"
493-
" void a();\n"
494-
"private:\n"
495-
" void b();\n"
496-
"};\n"
497-
"#endfile\n"
498-
"A::A() { }\n"
499-
"void A::b() { }");
500-
ASSERT_EQUALS("", errout_str());
501-
}
502-
503484
void derivedClass() {
504485
// skip warning in derived classes in case the base class is invisible
505486
check("class derived : public base\n"
@@ -791,7 +772,7 @@ class TestUnusedPrivateFunction : public TestFixture {
791772
"private:\n"
792773
" void Init();\n"
793774
"};\n"
794-
"#endfile\n"
775+
"#endfile\n" // TODO
795776
"void Fred::Init()\n"
796777
"{\n"
797778
"}");

0 commit comments

Comments
 (0)