Skip to content

Commit 7d01ad5

Browse files
committed
add test-clang-import that compares symboldatabases
1 parent e52c4f9 commit 7d01ad5

3 files changed

Lines changed: 65 additions & 15 deletions

File tree

lib/cppcheck.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,8 @@ unsigned int CppCheck::check(const std::string &path)
335335
#endif
336336

337337
std::string flags(lang + " ");
338-
if (Path::isCPP(path)) {
339-
if (mSettings.standards.cpp == Standards::CPP14)
340-
flags += "-std=c++14 ";
341-
else if (mSettings.standards.cpp == Standards::CPP17)
342-
flags += "-std=c++17 ";
343-
else if (mSettings.standards.cpp == Standards::CPP20)
344-
flags += "-std=c++20 ";
345-
}
338+
if (Path::isCPP(path) && !mSettings.standards.stdValue.empty())
339+
flags += "-std=" + mSettings.standards.stdValue + " ";
346340

347341
for (const std::string &i: mSettings.includePaths)
348342
flags += "-I" + i + " ";
@@ -413,12 +407,10 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
413407
temp.mSettings.userDefines += fs.cppcheckDefines();
414408
temp.mSettings.includePaths = fs.includePaths;
415409
temp.mSettings.userUndefs = fs.undefs;
416-
if (fs.standard == "c++14")
417-
temp.mSettings.standards.cpp = Standards::CPP14;
418-
else if (fs.standard == "c++17")
419-
temp.mSettings.standards.cpp = Standards::CPP17;
420-
else if (fs.standard == "c++20")
421-
temp.mSettings.standards.cpp = Standards::CPP20;
410+
if (fs.standard.find("++") != std::string::npos)
411+
temp.mSettings.standards.setCPP(fs.standard);
412+
else if (!fs.standard.empty())
413+
temp.mSettings.standards.setC(fs.standard);
422414
if (fs.platformType != Settings::Unspecified)
423415
temp.mSettings.platform(fs.platformType);
424416
if (mSettings.clang) {

lib/standards.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ struct Standards {
3838
/** C++ code standard */
3939
enum cppstd_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPPLatest=CPP20 } cpp;
4040

41+
/** --std value given on command line */
42+
std::string stdValue;
43+
4144
/** This constructor clear all the variables **/
4245
Standards() : c(C11), cpp(CPPLatest) {}
4346

4447
bool setC(const std::string& str) {
48+
stdValue = str;
4549
if (str == "c89" || str == "C89") {
4650
c = C89;
4751
return true;
@@ -68,6 +72,7 @@ struct Standards {
6872
return "";
6973
}
7074
bool setCPP(const std::string& str) {
75+
stdValue = str;
7176
if (str == "c++03" || str == "C++03") {
7277
cpp = CPP03;
7378
return true;
@@ -90,7 +95,7 @@ struct Standards {
9095
}
9196
return false;
9297
}
93-
const std::string getCPP() const {
98+
std::string getCPP() const {
9499
switch (cpp) {
95100
case CPP03:
96101
return "c++03";

test/cli/test-clang-import.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
# python -m pytest test-clang-import.py
3+
4+
import os
5+
import re
6+
import subprocess
7+
from testutils import create_gui_project_file, cppcheck
8+
9+
10+
def get_debug_section(title, stdout):
11+
s = re.sub(r'0x[0-9a-fA-F]+', '0x12345678', stdout)
12+
s = re.sub(r'nestedIn: Struct', 'nestedIn: Class', s)
13+
s = re.sub(r'classDef: struct', 'classDef: class', s)
14+
s = re.sub(r'isInline: [a-z]+', 'isInline: ---', s)
15+
s = re.sub(r'definedType: .*', 'definedType: ---', s)
16+
s = re.sub(r'needInitialization: .*', 'needInitialization: ---', s)
17+
s = re.sub(r'functionOf: .*', 'functionOf: ---', s)
18+
s = re.sub(r'0x12345678 Struct', '0x12345678 Class', s)
19+
pos1 = s.find(title)
20+
assert pos1 > 0
21+
pos1 = s.find('\n', pos1) + 1
22+
assert pos1 > 0
23+
pos2 = s.find("\n##", pos1)
24+
if pos2 < 0:
25+
return s[pos1:]
26+
return s[pos1:pos2-1]
27+
28+
29+
def check_symbol_database(code):
30+
# Only compare symboldatabases if clang is found in PATH
31+
try:
32+
subprocess.call(['clang', '--version'])
33+
except OSError:
34+
return
35+
36+
testfile = 'test.cpp'
37+
with open(testfile, 'w+t') as f:
38+
f.write(code)
39+
ret1, stdout1, stderr1 = cppcheck(['--clang', '--debug', '-v', testfile])
40+
ret2, stdout2, stderr2 = cppcheck(['--debug', '-v', testfile])
41+
os.remove(testfile)
42+
assert get_debug_section('### Symbol database', stdout1) == get_debug_section('### Symbol database', stdout2)
43+
44+
45+
def test1():
46+
check_symbol_database('int main(){return 0;}')
47+
48+
def test2():
49+
code = 'struct Foo { void f(); }; void Foo::f() {}'
50+
check_symbol_database(code)
51+
52+
53+

0 commit comments

Comments
 (0)