sepavloff updated this revision to Diff 292113. sepavloff added a comment. Rebased patch
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D78899/new/ https://reviews.llvm.org/D78899 Files: clang/include/clang/Driver/Compilation.h clang/lib/Driver/Compilation.cpp clang/unittests/Driver/ToolChainTest.cpp Index: clang/unittests/Driver/ToolChainTest.cpp =================================================================== --- clang/unittests/Driver/ToolChainTest.cpp +++ clang/unittests/Driver/ToolChainTest.cpp @@ -259,4 +259,29 @@ EXPECT_STREQ(Res.DriverMode, "--driver-mode=cl"); EXPECT_FALSE(Res.TargetIsValid); } + +TEST(ToolChainTest, PostCallback) { + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); + struct TestDiagnosticConsumer : public DiagnosticConsumer {}; + DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); + IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( + new llvm::vfs::InMemoryFileSystem); + + // The executable path must not exist. + Driver CCDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, + "clang LLVM compiler", InMemoryFileSystem); + CCDriver.setCheckInputsExist(false); + std::unique_ptr<Compilation> CC( + CCDriver.BuildCompilation({"/home/test/bin/clang", "foo.cpp"})); + bool CallbackHasCalled = false; + CC->setPostCallback( + [&](const Command &C, int Ret) { CallbackHasCalled = true; }); + const JobList &Jobs = CC->getJobs(); + auto &CmdCompile = Jobs.getJobs().front(); + const Command *FailingCmd = nullptr; + CC->ExecuteCommand(*CmdCompile, FailingCmd); + EXPECT_TRUE(CallbackHasCalled); +} + } // end anonymous namespace. Index: clang/lib/Driver/Compilation.cpp =================================================================== --- clang/lib/Driver/Compilation.cpp +++ clang/lib/Driver/Compilation.cpp @@ -193,6 +193,8 @@ std::string Error; bool ExecutionFailed; int Res = C.Execute(Redirects, &Error, &ExecutionFailed); + if (PostCallback) + PostCallback(C, Res); if (!Error.empty()) { assert(Res && "Error string set with 0 result code!"); getDriver().Diag(diag::err_drv_command_failure) << Error; Index: clang/include/clang/Driver/Compilation.h =================================================================== --- clang/include/clang/Driver/Compilation.h +++ clang/include/clang/Driver/Compilation.h @@ -115,6 +115,9 @@ /// Optional redirection for stdin, stdout, stderr. std::vector<Optional<StringRef>> Redirects; + /// Callback called after the command has been executed. + std::function<void(const Command &, int)> PostCallback; + /// Whether we're compiling for diagnostic purposes. bool ForDiagnostics = false; @@ -212,6 +215,10 @@ return FailureResultFiles; } + void setPostCallback(const std::function<void(const Command &, int)> &CB) { + PostCallback = CB; + } + /// Returns the sysroot path. StringRef getSysRoot() const;
Index: clang/unittests/Driver/ToolChainTest.cpp =================================================================== --- clang/unittests/Driver/ToolChainTest.cpp +++ clang/unittests/Driver/ToolChainTest.cpp @@ -259,4 +259,29 @@ EXPECT_STREQ(Res.DriverMode, "--driver-mode=cl"); EXPECT_FALSE(Res.TargetIsValid); } + +TEST(ToolChainTest, PostCallback) { + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); + struct TestDiagnosticConsumer : public DiagnosticConsumer {}; + DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer); + IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( + new llvm::vfs::InMemoryFileSystem); + + // The executable path must not exist. + Driver CCDriver("/home/test/bin/clang", "arm-linux-gnueabi", Diags, + "clang LLVM compiler", InMemoryFileSystem); + CCDriver.setCheckInputsExist(false); + std::unique_ptr<Compilation> CC( + CCDriver.BuildCompilation({"/home/test/bin/clang", "foo.cpp"})); + bool CallbackHasCalled = false; + CC->setPostCallback( + [&](const Command &C, int Ret) { CallbackHasCalled = true; }); + const JobList &Jobs = CC->getJobs(); + auto &CmdCompile = Jobs.getJobs().front(); + const Command *FailingCmd = nullptr; + CC->ExecuteCommand(*CmdCompile, FailingCmd); + EXPECT_TRUE(CallbackHasCalled); +} + } // end anonymous namespace. Index: clang/lib/Driver/Compilation.cpp =================================================================== --- clang/lib/Driver/Compilation.cpp +++ clang/lib/Driver/Compilation.cpp @@ -193,6 +193,8 @@ std::string Error; bool ExecutionFailed; int Res = C.Execute(Redirects, &Error, &ExecutionFailed); + if (PostCallback) + PostCallback(C, Res); if (!Error.empty()) { assert(Res && "Error string set with 0 result code!"); getDriver().Diag(diag::err_drv_command_failure) << Error; Index: clang/include/clang/Driver/Compilation.h =================================================================== --- clang/include/clang/Driver/Compilation.h +++ clang/include/clang/Driver/Compilation.h @@ -115,6 +115,9 @@ /// Optional redirection for stdin, stdout, stderr. std::vector<Optional<StringRef>> Redirects; + /// Callback called after the command has been executed. + std::function<void(const Command &, int)> PostCallback; + /// Whether we're compiling for diagnostic purposes. bool ForDiagnostics = false; @@ -212,6 +215,10 @@ return FailureResultFiles; } + void setPostCallback(const std::function<void(const Command &, int)> &CB) { + PostCallback = CB; + } + /// Returns the sysroot path. StringRef getSysRoot() const;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits