xref: /llvm-project/clang/lib/Frontend/ASTMerge.cpp (revision 71731d6b05db0fd5ff6c2e0641b050599706ae02)
1 //===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "clang/Frontend/ASTUnit.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Frontend/FrontendActions.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/ASTDiagnostic.h"
14 #include "clang/AST/ASTImporter.h"
15 #include "clang/Basic/Diagnostic.h"
16 
17 using namespace clang;
18 
19 ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
20                                                llvm::StringRef InFile) {
21   return AdaptedAction->CreateASTConsumer(CI, InFile);
22 }
23 
24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
25                                            llvm::StringRef Filename) {
26   // FIXME: This is a hack. We need a better way to communicate the
27   // AST file, compiler instance, and file name than member variables
28   // of FrontendAction.
29   AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30                                 takeCurrentASTUnit());
31   AdaptedAction->setCompilerInstance(&CI);
32   return AdaptedAction->BeginSourceFileAction(CI, Filename);
33 }
34 
35 void ASTMergeAction::ExecuteAction() {
36   CompilerInstance &CI = getCompilerInstance();
37   CI.getDiagnostics().getClient()->BeginSourceFile(
38                                          CI.getASTContext().getLangOptions());
39   CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40                                        &CI.getASTContext());
41   llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
42   for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
43     ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
44                                              CI.getFileSystemOpts(), false);
45     if (!Unit)
46       continue;
47 
48     // Reset the argument -> string function so that it has the AST
49     // context we want, since the Sema object created by
50     // LoadFromASTFile will override it.
51     CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
52                                          &CI.getASTContext());
53 
54     ASTImporter Importer(CI.getDiagnostics(),
55                          CI.getASTContext(),
56                          CI.getFileManager(),
57                          CI.getFileSystemOpts(),
58                          Unit->getASTContext(),
59                          Unit->getFileManager(),
60                          Unit->getFileSystemOpts());
61 
62     TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
63     for (DeclContext::decl_iterator D = TU->decls_begin(),
64                                  DEnd = TU->decls_end();
65          D != DEnd; ++D) {
66       // Don't re-import __va_list_tag, __builtin_va_list.
67       if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
68         if (IdentifierInfo *II = ND->getIdentifier())
69           if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
70             continue;
71 
72       Importer.Import(*D);
73     }
74 
75     delete Unit;
76   }
77 
78   AdaptedAction->ExecuteAction();
79   CI.getDiagnostics().getClient()->EndSourceFile();
80 }
81 
82 void ASTMergeAction::EndSourceFileAction() {
83   return AdaptedAction->EndSourceFileAction();
84 }
85 
86 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
87                                std::string *ASTFiles, unsigned NumASTFiles)
88   : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
89   assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
90 }
91 
92 ASTMergeAction::~ASTMergeAction() {
93   delete AdaptedAction;
94 }
95 
96 bool ASTMergeAction::usesPreprocessorOnly() const {
97   return AdaptedAction->usesPreprocessorOnly();
98 }
99 
100 bool ASTMergeAction::usesCompleteTranslationUnit() {
101   return AdaptedAction->usesCompleteTranslationUnit();
102 }
103 
104 bool ASTMergeAction::hasPCHSupport() const {
105   return AdaptedAction->hasPCHSupport();
106 }
107 
108 bool ASTMergeAction::hasASTFileSupport() const {
109   return AdaptedAction->hasASTFileSupport();
110 }
111 
112 bool ASTMergeAction::hasCodeCompletionSupport() const {
113   return AdaptedAction->hasCodeCompletionSupport();
114 }
115