@@ -28,28 +28,30 @@ SourceToHeaderRewriter::createFactory(llvm::raw_ostream *externalStream,
2828 llvm::raw_ostream *unnamedTypeDeclsStream,
2929 llvm::raw_ostream *wrapperStream,
3030 fs::path sourceFilePath,
31- bool forStubHeader) {
31+ bool forStubHeader,
32+ bool externFromStub) {
3233 if (Paths::isCXXFile (sourceFilePath)) {
3334 externalStream = nullptr ;
3435 internalStream = nullptr ;
3536 }
3637 fetcherInstance = std::make_unique<SourceToHeaderMatchCallback>(
37- projectContext, sourceFilePath, externalStream, internalStream, unnamedTypeDeclsStream, wrapperStream, typesHandler, forStubHeader);
38+ projectContext, sourceFilePath, externalStream, internalStream, unnamedTypeDeclsStream, wrapperStream,
39+ typesHandler, forStubHeader, externFromStub);
3840 finder = std::make_unique<clang::ast_matchers::MatchFinder>();
3941 finder->addMatcher (Matchers::anyToplevelDeclarationMatcher, fetcherInstance.get ());
4042 return clang::tooling::newFrontendActionFactory (finder.get ());
4143}
4244
4345SourceToHeaderRewriter::SourceDeclarations
44- SourceToHeaderRewriter::generateSourceDeclarations (const fs::path &sourceFilePath, bool forStubHeader) {
46+ SourceToHeaderRewriter::generateSourceDeclarations (const fs::path &sourceFilePath, bool forStubHeader, bool externFromStub ) {
4547 std::string externalDeclarations;
4648 llvm::raw_string_ostream externalStream (externalDeclarations);
4749 std::string internalDeclarations;
4850 llvm::raw_string_ostream internalStream (internalDeclarations);
4951 std::string unnamedTypeDeclarations;
5052 llvm::raw_string_ostream unnamedTypeDeclsStream (unnamedTypeDeclarations);
5153
52- auto factory = createFactory (&externalStream, &internalStream, &unnamedTypeDeclsStream, nullptr , sourceFilePath, forStubHeader);
54+ auto factory = createFactory (&externalStream, &internalStream, &unnamedTypeDeclsStream, nullptr , sourceFilePath, forStubHeader, externFromStub );
5355
5456 if (CollectionUtils::containsKey (*structsToDeclare, sourceFilePath)) {
5557 std::stringstream newContentStream;
@@ -72,9 +74,10 @@ SourceToHeaderRewriter::generateSourceDeclarations(const fs::path &sourceFilePat
7274
7375
7476std::string SourceToHeaderRewriter::generateTestHeader (const fs::path &sourceFilePath,
75- const Tests &test) {
77+ const Tests &test,
78+ bool externFromStub) {
7679 MEASURE_FUNCTION_EXECUTION_TIME
77- auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, false );
80+ auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, false , externFromStub );
7881
7982 if (Paths::isCXXFile (sourceFilePath)) {
8083 auto sourceFileToInclude = sourceFilePath;
@@ -112,7 +115,7 @@ std::string SourceToHeaderRewriter::generateStubHeader(const fs::path &sourceFil
112115 MEASURE_FUNCTION_EXECUTION_TIME
113116 LOG_IF_S (WARNING, Paths::isCXXFile (sourceFilePath))
114117 << " Stubs feature for C++ sources has not been tested thoroughly; some problems may occur" ;
115- auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, true );
118+ auto sourceDeclarations = generateSourceDeclarations (sourceFilePath, true , false );
116119 long long creationTime = TimeUtils::convertFileToSystemClock (fs::file_time_type::clock::now ())
117120 .time_since_epoch ()
118121 .count ();
@@ -133,19 +136,28 @@ std::string SourceToHeaderRewriter::generateWrapper(const fs::path &sourceFilePa
133136 }
134137 std::string result;
135138 llvm::raw_string_ostream wrapperStream (result);
136- auto factory = createFactory (nullptr , nullptr , nullptr , &wrapperStream, sourceFilePath, false );
139+ auto factory = createFactory (
140+ nullptr , nullptr , nullptr , &wrapperStream,
141+ sourceFilePath, false , false );
137142 clangToolRunner.run (sourceFilePath, factory.get ());
138143 wrapperStream.flush ();
139144 return result;
140145}
141146
142147void SourceToHeaderRewriter::generateTestHeaders (tests::TestsMap &tests,
143- ProgressWriter const *progressWriter) {
148+ const StubGen &stubGen,
149+ const CollectionUtils::MapFileTo<fs::path> &selectedTargets,
150+ ProgressWriter const *progressWriter) {
144151 std::string logMessage = " Generating headers for tests" ;
145152 LOG_S (DEBUG) << logMessage;
146- ExecUtils::doWorkWithProgress (tests, progressWriter, logMessage, [this ](auto &it) {
153+ ExecUtils::doWorkWithProgress (tests, progressWriter, logMessage,
154+ [this , &stubGen, &selectedTargets](auto &it) {
147155 fs::path const &sourceFilePath = it.first ;
148- tests::Tests &test = const_cast <tests::Tests &>(it.second );
149- test.headerCode = generateTestHeader (sourceFilePath, test);
156+ auto &test = const_cast <tests::Tests &>(it.second );
157+ auto iterator = selectedTargets.find (sourceFilePath);
158+ bool externFromStub =
159+ iterator != selectedTargets.end () &&
160+ CollectionUtils::contains (stubGen.getStubSources (iterator->second ), sourceFilePath);
161+ test.headerCode = generateTestHeader (sourceFilePath, test, externFromStub);
150162 });
151163}
0 commit comments