From 4295ae96cdf275cdda8bded9271960a4cac11fb2 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 1 Mar 2021 18:52:15 +0100 Subject: [clang][modules] Use extensible RTTI for ModuleFileExtension Clang exposes an interface for extending the PCM/PCH file format: `ModuleFileExtension`. Clang itself has only a single implementation of the interface: `TestModuleFileExtension` that can be instantiated via the `-ftest-module-file_extension=` command line argument (and is stored in `FrontendOptions::ModuleFileExtensions`). Clients of the Clang library can extend the PCM/PCH file format by pushing an instance of their extension class to the `FrontendOptions::ModuleFileExtensions` vector. When generating the `-ftest-module-file_extension=` command line argument from `FrontendOptions`, a downcast is used to distinguish between the Clang's testing extension and other (client) extensions. This functionality is enabled by LLVM-style RTTI. However, this style of RTTI is hard to extend, as it requires patching Clang (adding new case to the `ModuleFileExtensionKind` enum). This patch switches to the LLVM RTTI for open class hierarchies, which allows libClang users (e.g. Swift) to create implementations of `ModuleFileExtension` without patching Clang. (Documentation of the feature: https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html#rtti-for-open-class-hierarchies) Reviewed By: artemcm Differential Revision: https://reviews.llvm.org/D97702 --- .../unittests/Frontend/CompilerInvocationTest.cpp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'clang/unittests/Frontend/CompilerInvocationTest.cpp') diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp index b7f9ced..89e024d 100644 --- a/clang/unittests/Frontend/CompilerInvocationTest.cpp +++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -11,6 +11,7 @@ #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/TextDiagnosticBuffer.h" #include "clang/Lex/PreprocessorOptions.h" +#include "clang/Serialization/ModuleFileExtension.h" #include "llvm/Support/Host.h" #include "gmock/gmock.h" @@ -743,6 +744,58 @@ TEST_F(CommandLineTest, DigraphsEnabled) { ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fdigraphs"))); } +struct DummyModuleFileExtension + : public llvm::RTTIExtends { + static char ID; + + ModuleFileExtensionMetadata getExtensionMetadata() const override { + return {}; + }; + + llvm::hash_code hashExtension(llvm::hash_code Code) const override { + return {}; + } + + std::unique_ptr + createExtensionWriter(ASTWriter &Writer) override { + return {}; + } + + std::unique_ptr + createExtensionReader(const ModuleFileExtensionMetadata &Metadata, + ASTReader &Reader, serialization::ModuleFile &Mod, + const llvm::BitstreamCursor &Stream) override { + return {}; + } +}; + +char DummyModuleFileExtension::ID = 0; + +TEST_F(CommandLineTest, TestModuleFileExtension) { + const char *Args[] = {"-ftest-module-file-extension=first:2:1:0:first", + "-ftest-module-file-extension=second:3:2:1:second"}; + + ASSERT_TRUE(CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags)); + ASSERT_THAT(Invocation.getFrontendOpts().ModuleFileExtensions.size(), 2); + + // Exercise the check that only serializes instances of + // TestModuleFileExtension by providing an instance of another + // ModuleFileExtension subclass. + Invocation.getFrontendOpts().ModuleFileExtensions.push_back( + std::make_shared()); + + Invocation.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, + ContainsN(HasSubstr("-ftest-module-file-extension="), 2)); + ASSERT_THAT( + GeneratedArgs, + Contains(StrEq("-ftest-module-file-extension=first:2:1:0:first"))); + ASSERT_THAT( + GeneratedArgs, + Contains(StrEq("-ftest-module-file-extension=second:3:2:1:second"))); +} + TEST_F(CommandLineTest, RoundTrip) { // Testing one marshalled and one manually generated option from each // CompilerInvocation member. -- cgit v1.1