@@ -15,20 +15,26 @@ SourceToHeaderRewriter::SourceToHeaderRewriter(
15
15
utbot::ProjectContext projectContext,
16
16
const std::shared_ptr<CompilationDatabase> &compilationDatabase,
17
17
std::shared_ptr<Fetcher::FileToStringSet> structsToDeclare,
18
- fs::path serverBuildDir)
18
+ fs::path serverBuildDir,
19
+ const types::TypesHandler &typesHandler)
19
20
: projectContext(std::move(projectContext)),
20
21
clangToolRunner(compilationDatabase), structsToDeclare(structsToDeclare),
21
- serverBuildDir(std::move(serverBuildDir)) {
22
+ serverBuildDir(std::move(serverBuildDir)), typesHandler(typesHandler) {
22
23
}
23
24
24
25
std::unique_ptr<clang::tooling::FrontendActionFactory>
25
26
SourceToHeaderRewriter::createFactory (llvm::raw_ostream *externalStream,
26
27
llvm::raw_ostream *internalStream,
28
+ llvm::raw_ostream *unnamedTypeDeclsStream,
27
29
llvm::raw_ostream *wrapperStream,
28
30
fs::path sourceFilePath,
29
31
bool forStubHeader) {
32
+ if (Paths::isCXXFile (sourceFilePath)) {
33
+ externalStream = nullptr ;
34
+ internalStream = nullptr ;
35
+ }
30
36
fetcherInstance = std::make_unique<SourceToHeaderMatchCallback>(
31
- projectContext, sourceFilePath, externalStream, internalStream, wrapperStream, forStubHeader);
37
+ projectContext, sourceFilePath, externalStream, internalStream, unnamedTypeDeclsStream, wrapperStream, typesHandler , forStubHeader);
32
38
finder = std::make_unique<clang::ast_matchers::MatchFinder>();
33
39
finder->addMatcher (Matchers::anyToplevelDeclarationMatcher, fetcherInstance.get ());
34
40
return clang::tooling::newFrontendActionFactory (finder.get ());
@@ -40,8 +46,10 @@ SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePat
40
46
llvm::raw_string_ostream externalStream (externalDeclarations);
41
47
std::string internalDeclarations;
42
48
llvm::raw_string_ostream internalStream (internalDeclarations);
49
+ std::string unnamedTypeDeclarations;
50
+ llvm::raw_string_ostream unnamedTypeDeclsStream (unnamedTypeDeclarations);
43
51
44
- auto factory = createFactory (&externalStream, &internalStream, nullptr , sourceFilePath, forStubHeader);
52
+ auto factory = createFactory (&externalStream, &internalStream, &unnamedTypeDeclsStream, nullptr , sourceFilePath, forStubHeader);
45
53
46
54
if (CollectionUtils::containsKey (*structsToDeclare, sourceFilePath)) {
47
55
std::stringstream newContentStream;
@@ -57,14 +65,17 @@ SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePat
57
65
}
58
66
externalStream.flush ();
59
67
internalStream.flush ();
68
+ unnamedTypeDeclsStream.flush ();
60
69
61
- return { externalDeclarations, internalDeclarations };
70
+ return { externalDeclarations, internalDeclarations, unnamedTypeDeclarations };
62
71
}
63
72
64
73
65
74
std::string SourceToHeaderRewriter::generateTestHeader (const fs::path &sourceFilePath,
66
75
const Tests &test) {
67
76
MEASURE_FUNCTION_EXECUTION_TIME
77
+ auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, false );
78
+
68
79
if (Paths::isCXXFile (sourceFilePath)) {
69
80
auto sourceFileToInclude = sourceFilePath;
70
81
if (test.mainHeader .has_value ()) {
@@ -73,12 +84,11 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
73
84
}
74
85
sourceFileToInclude = fs::relative (sourceFilePath, test.testHeaderFilePath .parent_path ());
75
86
return StringUtils::stringFormat (" #define main main__\n\n "
76
- " #include \" %s\"\n\n " ,
77
- sourceFileToInclude);
87
+ " #include \" %s\"\n\n "
88
+ " %s\n " ,
89
+ sourceFileToInclude, sourceDeclarations.unnamedTypeDeclarations );
78
90
}
79
91
80
- auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, false );
81
-
82
92
return StringUtils::stringFormat (
83
93
" %s\n "
84
94
" namespace %s {\n "
@@ -88,13 +98,14 @@ std::string SourceToHeaderRewriter::generateTestHeader(const fs::path &sourceFil
88
98
" %s\n "
89
99
" %s\n "
90
100
" %s\n "
91
- " }\n "
92
- " %s\n " ,
101
+ " %s\n "
102
+ " \n %s"
103
+ " }\n " ,
93
104
Copyright::GENERATED_C_CPP_FILE_HEADER, PrinterUtils::TEST_NAMESPACE,
94
105
NameDecorator::DEFINES_CODE, PrinterUtils::DEFINES_FOR_C_KEYWORDS,
95
106
PrinterUtils::KNOWN_IMPLICIT_RECORD_DECLS_CODE,
96
107
sourceDeclarations.externalDeclarations , sourceDeclarations.internalDeclarations ,
97
- NameDecorator::UNDEF_WCHAR_T, NameDecorator::UNDEFS_CODE);
108
+ NameDecorator::UNDEF_WCHAR_T, NameDecorator::UNDEFS_CODE, sourceDeclarations. unnamedTypeDeclarations );
98
109
}
99
110
100
111
std::string SourceToHeaderRewriter::generateStubHeader (const fs::path &sourceFilePath) {
@@ -122,7 +133,7 @@ std::string SourceToHeaderRewriter::generateWrapper(const fs::path &sourceFilePa
122
133
}
123
134
std::string result;
124
135
llvm::raw_string_ostream wrapperStream (result);
125
- auto factory = createFactory (nullptr , nullptr , &wrapperStream, sourceFilePath, false );
136
+ auto factory = createFactory (nullptr , nullptr , nullptr , &wrapperStream, sourceFilePath, false );
126
137
clangToolRunner.run (sourceFilePath, factory.get ());
127
138
wrapperStream.flush ();
128
139
return result;
0 commit comments