From 96d406d68d249d9c6503f6b1199b119ed6498760 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Fri, 20 Nov 2015 03:36:21 +0000 Subject: [PATCH] [libclang] Make sure to use the raw module format for libclang parsing. Fixes crash when passing '-gmodules' in the compiler options. rdar://23588717 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@253645 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Frontend/ASTUnit.h | 3 +++ lib/Frontend/ASTUnit.cpp | 4 ++++ test/Index/annotate-module.m | 2 ++ test/Index/complete-modules.m | 1 + test/Index/index-module.m | 2 ++ tools/libclang/CIndex.cpp | 4 +++- tools/libclang/Indexing.cpp | 4 ++++ 7 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index 7d9d121038..d326a53dc2 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -827,6 +827,8 @@ public: /// /// \param ResourceFilesPath - The path to the compiler resource files. /// + /// \param ModuleFormat - If provided, uses the specific module format. + /// /// \param ErrAST - If non-null and parsing failed without any AST to return /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit /// mainly to allow the caller to see the diagnostics. @@ -845,6 +847,7 @@ public: bool IncludeBriefCommentsInCodeCompletion = false, bool AllowPCHWithCompilerErrors = false, bool SkipFunctionBodies = false, bool UserFilesAreVolatile = false, bool ForSerialization = false, + llvm::Optional ModuleFormat = llvm::None, std::unique_ptr *ErrAST = nullptr); /// \brief Reparse the source files using the same command-line options that diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 6b89447305..0d5dd53658 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1934,6 +1934,7 @@ ASTUnit *ASTUnit::LoadFromCommandLine( bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies, bool UserFilesAreVolatile, bool ForSerialization, + llvm::Optional ModuleFormat, std::unique_ptr *ErrAST) { assert(Diags.get() && "no DiagnosticsEngine was provided"); @@ -1967,6 +1968,9 @@ ASTUnit *ASTUnit::LoadFromCommandLine( CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies; + if (ModuleFormat) + CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue(); + // Create the AST unit. std::unique_ptr AST; AST.reset(new ASTUnit(false)); diff --git a/test/Index/annotate-module.m b/test/Index/annotate-module.m index 456a192f42..24dce3f290 100644 --- a/test/Index/annotate-module.m +++ b/test/Index/annotate-module.m @@ -6,6 +6,8 @@ int glob; // RUN: rm -rf %t.cache // RUN: c-index-test -test-annotate-tokens=%s:2:1:5:1 %s -fmodules-cache-path=%t.cache -fmodules -F %S/../Modules/Inputs \ // RUN: | FileCheck %s +// RUN: c-index-test -test-annotate-tokens=%s:2:1:5:1 %s -fmodules-cache-path=%t.cache -fmodules -gmodules -F %S/../Modules/Inputs \ +// RUN: | FileCheck %s // CHECK: Punctuation: "#" [2:1 - 2:2] inclusion directive=[[INC_DIR:DependsOnModule[/\\]DependsOnModule\.h \(.*/Modules/Inputs/DependsOnModule\.framework[/\\]Headers[/\\]DependsOnModule.h\)]] // CHECK-NEXT: Identifier: "include" [2:2 - 2:9] inclusion directive=[[INC_DIR]] diff --git a/test/Index/complete-modules.m b/test/Index/complete-modules.m index 175cf3db2c..21f0e4bafe 100644 --- a/test/Index/complete-modules.m +++ b/test/Index/complete-modules.m @@ -5,6 +5,7 @@ // RUN: rm -rf %t // RUN: c-index-test -code-completion-at=%s:4:9 -fmodules-cache-path=%t -fmodules -F %S/Inputs/Frameworks -I %S/Inputs/Headers %s | FileCheck -check-prefix=CHECK-TOP-LEVEL %s +// RUN: c-index-test -code-completion-at=%s:4:9 -fmodules-cache-path=%t -fmodules -gmodules -F %S/Inputs/Frameworks -I %S/Inputs/Headers %s | FileCheck -check-prefix=CHECK-TOP-LEVEL %s // CHECK-TOP-LEVEL: ModuleImport:{TypedText Framework} (50) // CHECK-TOP-LEVEL: ModuleImport:{TypedText LibA} (50) // CHECK-TOP-LEVEL: ModuleImport:{TypedText nested} (50) diff --git a/test/Index/index-module.m b/test/Index/index-module.m index a973e91af8..ff512592b6 100644 --- a/test/Index/index-module.m +++ b/test/Index/index-module.m @@ -6,6 +6,8 @@ int glob; // RUN: rm -rf %t.cache // RUN: c-index-test -index-file %s -fmodules-cache-path=%t.cache -fmodules -F %S/../Modules/Inputs \ // RUN: -Xclang -fdisable-module-hash | FileCheck %s +// RUN: c-index-test -index-file %s -fmodules-cache-path=%t.cache -fmodules -gmodules -F %S/../Modules/Inputs \ +// RUN: -Xclang -fdisable-module-hash | FileCheck %s // CHECK-NOT: [indexDeclaration] // CHECK: [ppIncludedFile]: {{.*}}/Modules/Inputs/DependsOnModule.framework{{[/\\]}}Headers{{[/\\]}}DependsOnModule.h | name: "DependsOnModule/DependsOnModule.h" | hash loc: 2:1 | isImport: 0 | isAngled: 1 | isModule: 1 diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index c50b41408a..a49437b738 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -3119,7 +3119,9 @@ clang_parseTranslationUnit_Impl(CXIndex CIdx, const char *source_filename, /*RemappedFilesKeepOriginalName=*/true, PrecompilePreamble, TUKind, CacheCodeCompletionResults, IncludeBriefCommentsInCodeCompletion, /*AllowPCHWithCompilerErrors=*/true, SkipFunctionBodies, - /*UserFilesAreVolatile=*/true, ForSerialization, &ErrUnit)); + /*UserFilesAreVolatile=*/true, ForSerialization, + CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(), + &ErrUnit)); // Early failures in LoadFromCommandLine may return with ErrUnit unset. if (!Unit && !ErrUnit) diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index 65ffbdf44a..d198f404a0 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -566,6 +566,10 @@ static CXErrorCode clang_indexSourceFile_Impl( if (index_options & CXIndexOpt_SuppressWarnings) CInvok->getDiagnosticOpts().IgnoreWarnings = true; + // Make sure to use the raw module format. + CInvok->getHeaderSearchOpts().ModuleFormat = + CXXIdx->getPCHContainerOperations()->getRawReader().getFormat(); + ASTUnit *Unit = ASTUnit::create(CInvok.get(), Diags, CaptureDiagnostics, /*UserFilesAreVolatile=*/true); if (!Unit) -- 2.40.0