Skip to content

Commit 89e775b

Browse files
committed
removed support for #file/#endfile
1 parent 21de4fa commit 89e775b

5 files changed

Lines changed: 168 additions & 116 deletions

File tree

lib/cppcheck.cpp

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

1139-
if (startsWith(codeWithoutCfg,"#file"))
1140-
codeWithoutCfg.insert(0U, "//");
11411139
std::string::size_type pos = 0;
1142-
while ((pos = codeWithoutCfg.find("\n#file",pos)) != std::string::npos)
1143-
codeWithoutCfg.insert(pos+1U, "//");
1144-
pos = 0;
1145-
while ((pos = codeWithoutCfg.find("\n#endfile",pos)) != std::string::npos)
1146-
codeWithoutCfg.insert(pos+1U, "//");
1147-
pos = 0;
11481140
while ((pos = codeWithoutCfg.find(Preprocessor::macroChar,pos)) != std::string::npos)
11491141
codeWithoutCfg[pos] = ' ';
11501142
mErrorLogger.reportOut(codeWithoutCfg, Color::Reset);

test/cli/other_test.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4240,6 +4240,117 @@ def test_no_valid_configuration(tmp_path):
42404240
]
42414241

42424242

4243+
# The implementation for "A::a" is missing - so don't check if "A::b" is used or not
4244+
def test_unused_private_function_incomplete_impl(tmpdir):
4245+
test_inc = os.path.join(tmpdir, 'test.h')
4246+
with open(test_inc, 'wt') as f:
4247+
f.write(
4248+
"""
4249+
class A
4250+
{
4251+
public:
4252+
A();
4253+
void a();
4254+
private:
4255+
void b();
4256+
};
4257+
""")
4258+
4259+
test_file = os.path.join(tmpdir, 'test.cpp')
4260+
with open(test_file, 'wt') as f:
4261+
f.write(
4262+
"""
4263+
#include "test.h"
4264+
4265+
A::A() { }
4266+
void A::b() { }
4267+
""")
4268+
4269+
args = [
4270+
'-q',
4271+
'--template=simple',
4272+
'--enable=style',
4273+
'--suppress=functionStatic', # we do not care about this - this was converted from TestUnusedPrivateFunction
4274+
test_file
4275+
]
4276+
4277+
ret, stdout, stderr = cppcheck(args)
4278+
assert stdout == ''
4279+
assert stderr.splitlines() == []
4280+
assert ret == 0, stdout
4281+
4282+
4283+
def test_unused_private_function_multi_file(tmpdir): # ticket #2567
4284+
test_inc = os.path.join(tmpdir, 'test.h')
4285+
with open(test_inc, 'wt') as f:
4286+
f.write(
4287+
"""
4288+
struct Fred
4289+
{
4290+
Fred()
4291+
{
4292+
Init();
4293+
}
4294+
private:
4295+
void Init();
4296+
};
4297+
""")
4298+
4299+
test_file = os.path.join(tmpdir, 'test.cpp')
4300+
with open(test_file, 'wt') as f:
4301+
f.write(
4302+
"""
4303+
#include "test.h"
4304+
4305+
void Fred::Init()
4306+
{
4307+
}
4308+
""")
4309+
4310+
args = [
4311+
'-q',
4312+
'--template=simple',
4313+
'--enable=style',
4314+
'--suppress=functionStatic', # we do not care about this - this was converted from TestUnusedPrivateFunction
4315+
test_file
4316+
]
4317+
4318+
ret, stdout, stderr = cppcheck(args)
4319+
assert stdout == ''
4320+
assert stderr.splitlines() == []
4321+
assert ret == 0, stdout
4322+
4323+
4324+
def test_missing_doublequote_include(tmpdir):
4325+
test_inc = os.path.join(tmpdir, 'abc.h')
4326+
with open(test_inc, 'wt') as f:
4327+
f.write(
4328+
'''
4329+
#define a
4330+
"
4331+
''')
4332+
4333+
test_file = os.path.join(tmpdir, 'test.cpp')
4334+
with open(test_file, 'wt') as f:
4335+
f.write(
4336+
"""
4337+
#include "abc.h"
4338+
""")
4339+
4340+
args = [
4341+
'-q',
4342+
'--template=simple',
4343+
test_file
4344+
]
4345+
4346+
ret, stdout, stderr = cppcheck(args)
4347+
assert stdout == ''
4348+
assert stderr.splitlines() == [
4349+
f"{test_inc}:3:1: error: No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported. [syntaxError]"
4350+
]
4351+
assert ret == 0, stdout
4352+
4353+
42434354
def test_no_valid_configuration_check_config(tmp_path):
42444355
test_file = tmp_path / 'test.c'
42454356
with open(test_file, "w") as f:

test/testpreprocessor.cpp

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ class TestPreprocessor : public TestFixture {
5050
TestPreprocessor() : TestFixture("TestPreprocessor") {}
5151

5252
private:
53+
#define expandMacros(...) expandMacros_(__FILE__, __LINE__, __VA_ARGS__)
5354
template<size_t size>
54-
std::string expandMacros(const char (&code)[size], ErrorLogger &errorLogger) const {
55+
std::string expandMacros_(const char* file, int line, const char (&code)[size], ErrorLogger &errorLogger) const {
5556
simplecpp::OutputList outputList;
5657
std::vector<std::string> files;
5758
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
5859
Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
59-
ASSERT(p.loadFiles(files));
60+
ASSERT_LOC(p.loadFiles(files), file, line);
6061
simplecpp::TokenList tokens2 = p.preprocess("", files, outputList);
6162
(void)p.reportOutput(outputList, true);
6263
return tokens2.stringify();
@@ -140,7 +141,7 @@ class TestPreprocessor : public TestFixture {
140141
cfgs = preprocessor.getConfigs();
141142
for (const std::string & config : cfgs) {
142143
try {
143-
const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr);
144+
const bool writeLocations = (strstr(code, "#include") != nullptr);
144145
cfgcode[config] = preprocessor.getcode(config, files, writeLocations);
145146
} catch (const simplecpp::Output &) {
146147
cfgcode[config] = "";
@@ -392,7 +393,7 @@ class TestPreprocessor : public TestFixture {
392393
std::vector<std::string> files;
393394
simplecpp::OutputList outputList;
394395
simplecpp::TokenList tokens(code,files,"test.c",&outputList);
395-
Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C); // TODO: do we need to consider #file?
396+
Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C);
396397
ASSERT(preprocessor.loadFiles(files));
397398
ASSERT(!preprocessor.reportOutput(outputList, true));
398399
preprocessor.removeComments();
@@ -407,7 +408,7 @@ class TestPreprocessor : public TestFixture {
407408
std::size_t getHash(const char (&code)[size]) {
408409
std::vector<std::string> files;
409410
simplecpp::TokenList tokens(code,files,"test.c");
410-
Preprocessor preprocessor(tokens, settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file?
411+
Preprocessor preprocessor(tokens, settingsDefault, *this, Standards::Language::C);
411412
ASSERT(preprocessor.loadFiles(files));
412413
preprocessor.removeComments();
413414
return preprocessor.calculateHash("");
@@ -472,16 +473,19 @@ class TestPreprocessor : public TestFixture {
472473
void error4() {
473474
// In included file
474475
{
476+
ScopedFile header("ab.h", "#error hello world!\n");
475477
const auto settings = dinit(Settings, $.userDefines = "TEST");
476-
const char code[] = "#file \"ab.h\"\n#error hello world!\n#endfile";
478+
const char code[] = "#include \"ab.h\"";
477479
(void)getcodeforcfg(settings, *this, code, "TEST", "test.c");
478480
ASSERT_EQUALS("[ab.h:1:2]: (error) #error hello world! [preprocessorErrorDirective]\n", errout_str());
479481
}
480482

481483
// After including a file
482484
{
485+
ScopedFile header("ab.h", "");
483486
const auto settings = dinit(Settings, $.userDefines = "TEST");
484-
const char code[] = "#file \"ab.h\"\n\n#endfile\n#error aaa";
487+
const char code[] = "#include \"ab.h\"\n"
488+
"#error aaa";
485489
(void)getcodeforcfg(settings, *this, code, "TEST", "test.c");
486490
ASSERT_EQUALS("[test.c:2:2]: (error) #error aaa [preprocessorErrorDirective]\n", errout_str());
487491
}
@@ -584,35 +588,35 @@ class TestPreprocessor : public TestFixture {
584588
}
585589

586590
void includeguard1() {
591+
ScopedFile header("abc.h",
592+
"#ifndef abcH\n"
593+
"#define abcH\n"
594+
"#endif\n");
587595
// Handling include guards..
588-
const char filedata[] = "#file \"abc.h\"\n"
589-
"#ifndef abcH\n"
590-
"#define abcH\n"
591-
"#endif\n"
592-
"#endfile\n"
596+
const char filedata[] = "#include \"abc.h\"\n"
593597
"#ifdef ABC\n"
594598
"#endif";
595599
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
596600
}
597601

598602
void includeguard2() {
603+
ScopedFile header("abc.h",
604+
"foo\n"
605+
"#ifdef ABC\n"
606+
"\n"
607+
"#endif\n");
599608
// Handling include guards..
600-
const char filedata[] = "#file \"abc.h\"\n"
601-
"foo\n"
602-
"#ifdef ABC\n"
603-
"\n"
604-
"#endif\n"
605-
"#endfile\n";
609+
const char filedata[] = "#include \"abc.h\"\n";
606610
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
607611
}
608612

609613

610614
void ifdefwithfile() {
615+
ScopedFile header("abc.h", "class A{};/*\n\n\n\n\n\n\n*/\n");
616+
611617
// Handling include guards..
612618
const char filedata[] = "#ifdef ABC\n"
613-
"#file \"abc.h\"\n"
614-
"class A{};/*\n\n\n\n\n\n\n*/\n"
615-
"#endfile\n"
619+
"#include \"abc.h\"\n"
616620
"#endif\n"
617621
"int main() {}\n";
618622

@@ -1576,22 +1580,9 @@ class TestPreprocessor : public TestFixture {
15761580
}
15771581

15781582
{
1579-
const char filedata[] = "#file \"abc.h\"\n"
1580-
"#define a\n"
1581-
"\"\n"
1582-
"#endfile\n";
1583-
1584-
// expand macros..
1585-
const std::string actual(expandMacros(filedata, *this));
1586-
1587-
ASSERT_EQUALS("", actual);
1588-
ASSERT_EQUALS("[abc.h:2:1]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported. [syntaxError]\n", errout_str());
1589-
}
1590-
1591-
{
1592-
const char filedata[] = "#file \"abc.h\"\n"
1593-
"#define a\n"
1594-
"#endfile\n"
1583+
ScopedFile header("abc.h",
1584+
"#define a\n");
1585+
const char filedata[] = "#include \"abc.h\"\n"
15951586
"\"\n";
15961587

15971588
// expand macros..
@@ -2286,14 +2277,14 @@ class TestPreprocessor : public TestFixture {
22862277
}
22872278

22882279
void getConfigs7e() {
2280+
ScopedFile header("test.h",
2281+
"#ifndef test_h\n"
2282+
"#define test_h\n"
2283+
"#ifdef ABC\n"
2284+
"#endif\n"
2285+
"#endif\n");
22892286
const char filedata[] = "#ifdef ABC\n"
2290-
"#file \"test.h\"\n"
2291-
"#ifndef test_h\n"
2292-
"#define test_h\n"
2293-
"#ifdef ABC\n"
2294-
"#endif\n"
2295-
"#endif\n"
2296-
"#endfile\n"
2287+
"#include \"test.h\"\n"
22972288
"#endif\n";
22982289
ASSERT_EQUALS("\nABC=ABC\n", getConfigsStr(filedata));
22992290
}
@@ -2315,12 +2306,12 @@ class TestPreprocessor : public TestFixture {
23152306
}
23162307

23172308
void getConfigs11() { // #9832 - include guards
2318-
const char filedata[] = "#file \"test.h\"\n"
2319-
"#if !defined(test_h)\n"
2320-
"#define test_h\n"
2321-
"123\n"
2322-
"#endif\n"
2323-
"#endfile\n";
2309+
ScopedFile header("test.h",
2310+
"#if !defined(test_h)\n"
2311+
"#define test_h\n"
2312+
"123\n"
2313+
"#endif\n");
2314+
const char filedata[] = "#include \"test.h\"\n";
23242315
ASSERT_EQUALS("\n", getConfigsStr(filedata));
23252316
}
23262317

test/testtokenize.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8821,14 +8821,11 @@ class TestTokenizer : public TestFixture {
88218821
}
88228822

88238823
void testDirectiveIncludeLocations() {
8824+
ScopedFile inc1("inc1.h", "#define macro2 val\n#include \"inc2.h\"\n#define macro4 val\n");
8825+
ScopedFile inc2("inc2.h", "#define macro3 val\n");
8826+
// TODO: preprocess?
88248827
const char filedata[] = "#define macro1 val\n"
8825-
"#file \"inc1.h\"\n"
8826-
"#define macro2 val\n"
8827-
"#file \"inc2.h\"\n"
8828-
"#define macro3 val\n"
8829-
"#endfile\n"
8830-
"#define macro4 val\n"
8831-
"#endfile\n"
8828+
"#include \"inc1.h\"\n"
88328829
"#define macro5 val\n";
88338830
const char dumpdata[] = " <directivelist>\n"
88348831
" <directive file=\"test.c\" linenr=\"1\" str=\"#define macro1 val\">\n"
@@ -8839,8 +8836,14 @@ class TestTokenizer : public TestFixture {
88398836
" </directive>\n"
88408837
" <directive file=\"test.c\" linenr=\"2\" str=\"#include &quot;inc1.h&quot;\">\n"
88418838
" <token column=\"1\" str=\"#\"/>\n"
8842-
" <token column=\"2\" str=\"file\"/>\n"
8843-
" <token column=\"7\" str=\"&quot;inc1.h&quot;\"/>\n"
8839+
" <token column=\"2\" str=\"include\"/>\n"
8840+
" <token column=\"10\" str=\"&quot;inc1.h&quot;\"/>\n"
8841+
" </directive>\n"
8842+
" <directive file=\"test.c\" linenr=\"3\" str=\"#define macro5 val\">\n"
8843+
" <token column=\"1\" str=\"#\"/>\n"
8844+
" <token column=\"2\" str=\"define\"/>\n"
8845+
" <token column=\"9\" str=\"macro5\"/>\n"
8846+
" <token column=\"16\" str=\"val\"/>\n"
88448847
" </directive>\n"
88458848
" <directive file=\"inc1.h\" linenr=\"1\" str=\"#define macro2 val\">\n"
88468849
" <token column=\"1\" str=\"#\"/>\n"
@@ -8850,25 +8853,19 @@ class TestTokenizer : public TestFixture {
88508853
" </directive>\n"
88518854
" <directive file=\"inc1.h\" linenr=\"2\" str=\"#include &quot;inc2.h&quot;\">\n"
88528855
" <token column=\"1\" str=\"#\"/>\n"
8853-
" <token column=\"2\" str=\"file\"/>\n"
8854-
" <token column=\"7\" str=\"&quot;inc2.h&quot;\"/>\n"
8855-
" </directive>\n"
8856-
" <directive file=\"inc2.h\" linenr=\"1\" str=\"#define macro3 val\">\n"
8857-
" <token column=\"1\" str=\"#\"/>\n"
8858-
" <token column=\"2\" str=\"define\"/>\n"
8859-
" <token column=\"9\" str=\"macro3\"/>\n"
8860-
" <token column=\"16\" str=\"val\"/>\n"
8856+
" <token column=\"2\" str=\"include\"/>\n"
8857+
" <token column=\"10\" str=\"&quot;inc2.h&quot;\"/>\n"
88618858
" </directive>\n"
88628859
" <directive file=\"inc1.h\" linenr=\"3\" str=\"#define macro4 val\">\n"
88638860
" <token column=\"1\" str=\"#\"/>\n"
88648861
" <token column=\"2\" str=\"define\"/>\n"
88658862
" <token column=\"9\" str=\"macro4\"/>\n"
88668863
" <token column=\"16\" str=\"val\"/>\n"
88678864
" </directive>\n"
8868-
" <directive file=\"test.c\" linenr=\"3\" str=\"#define macro5 val\">\n"
8865+
" <directive file=\"inc2.h\" linenr=\"1\" str=\"#define macro3 val\">\n"
88698866
" <token column=\"1\" str=\"#\"/>\n"
88708867
" <token column=\"2\" str=\"define\"/>\n"
8871-
" <token column=\"9\" str=\"macro5\"/>\n"
8868+
" <token column=\"9\" str=\"macro3\"/>\n"
88728869
" <token column=\"16\" str=\"val\"/>\n"
88738870
" </directive>\n"
88748871
" </directivelist>\n"

0 commit comments

Comments
 (0)