|
| 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