C++: Turn header variant tests that use PCH files into integration tests

These tests currently test a code path in the extractor that only exists for
these tests. By turning them into integration tests, we actually use the
code path that normal database creation uses.
This commit is contained in:
Jeroen Ketema
2025-04-29 13:16:40 +02:00
parent e5d296180b
commit 7f9020282d
36 changed files with 62 additions and 53 deletions

View File

@@ -0,0 +1,2 @@
#include "a.h"
#define FOUR 4

View File

@@ -0,0 +1 @@
#define ONE 1

View File

@@ -0,0 +1,7 @@
#define TWO 2
#include "b.h"
int main() {
return ONE + TWO + THREE + FOUR;
}
// semmle-extractor-options: --clang -include-pch ${testdir}/clang-pch.testproj/a.pch -Iextra_dummy_path

View File

@@ -0,0 +1 @@
#define THREE 3

View File

@@ -0,0 +1,3 @@
int main() {
return ONE + FOUR;
}

View File

@@ -0,0 +1,7 @@
| b.c:5:3:5:34 | return ... | 10 |
| c.c:2:3:2:20 | return ... | 5 |
| e.c:2:3:2:19 | return ... | 17 |
| g.c:3:3:3:12 | return ... | 20 |
| i.c:3:3:3:12 | return ... | 30 |
| i.c:8:3:8:12 | return ... | 31 |
| i.c:13:3:13:12 | return ... | 32 |

View File

@@ -0,0 +1,4 @@
import cpp
from ReturnStmt rs
select rs, rs.getExpr().getValue()

View File

@@ -0,0 +1 @@
#import "d.h"

View File

@@ -0,0 +1 @@
enum { SEVENTEEN = 17 };

View File

@@ -0,0 +1,3 @@
int main() {
return SEVENTEEN;
}

View File

@@ -0,0 +1,5 @@
#if 1
#pragma hdrstop
extern int x;
#define SEEN_F
#endif

View File

@@ -0,0 +1,5 @@
#ifdef SEEN_F
static int g() {
return 20;
}
#endif

View File

@@ -0,0 +1,4 @@
#include "h1.h"
#pragma hdrstop
#include "h2.h"
#define SEEN_H

View File

@@ -0,0 +1,3 @@
#define H1

View File

@@ -0,0 +1,3 @@
#define H2

View File

@@ -0,0 +1,15 @@
#ifdef SEEN_H
static int h() {
return 30;
}
#endif
#ifdef H1
static int h1() {
return 31;
}
#endif
#ifdef H2
static int h2() {
return 32;
}
#endif

View File

@@ -0,0 +1,16 @@
import os
def test(codeql, cpp):
os.mkdir("pch")
codeql.database.create(command=[
f'"{cpp.get_tool("extractor")}" --mimic-clang -emit-pch -o pch/a.pch a.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -include-pch pch/a.pch -Iextra_dummy_path b.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -include pch/a -Iextra_dummy_path c.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -emit-pch -o pch/d.pch d.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -include-pch pch/d.pch e.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -emit-pch -o pch/f.pch f.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -include-pch pch/f.pch g.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -emit-pch -o pch/h.pch h.c',
f'"{cpp.get_tool("extractor")}" --mimic-clang -include-pch pch/h.pch i.c',
])

View File

@@ -0,0 +1 @@
#include "a.h"

View File

@@ -0,0 +1 @@
#define A 1

View File

@@ -0,0 +1,6 @@
#pragma hdrstop
#include "b.h"
int b() {
return A;
}

View File

@@ -0,0 +1,6 @@
#include "d.h"
#include "c.h"
int c() {
return A;
}

View File

@@ -0,0 +1,2 @@
| b | 1 |
| c | 1 |

View File

@@ -0,0 +1,4 @@
import cpp
from ReturnStmt rs
select rs.getEnclosingFunction().getName(), rs.getExpr().(Literal).getValue()

View File

@@ -0,0 +1,10 @@
import os
def test(codeql, cpp):
os.mkdir("pch")
codeql.database.create(command=[
f'"{cpp.get_tool("extractor")}" --mimic-cl /Yca.h /Fppch/a.pch a.c',
f'"{cpp.get_tool("extractor")}" --mimic-cl /Yub.h /Fppch/a.pch b.c',
f'"{cpp.get_tool("extractor")}" --mimic-cl /Yuc.h /Fppch/a.pch c.c',
])