xref: /llvm-project/clang/lib/Frontend/ASTMerge.cpp (revision fcb2e6ddea39b8f235fa00fc0b245ce01d76956b)
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::LoadFromPCHFile(ASTFiles[I], Diags, false);
44     if (!Unit)
45       continue;
46 
47     ASTImporter Importer(CI.getDiagnostics(),
48                          CI.getASTContext(),
49                          CI.getFileManager(),
50                          Unit->getASTContext(),
51                          Unit->getFileManager());
52 
53     TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
54     for (DeclContext::decl_iterator D = TU->decls_begin(),
55                                  DEnd = TU->decls_end();
56          D != DEnd; ++D) {
57       // Don't re-import __va_list_tag, __builtin_va_list.
58       if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
59         if (IdentifierInfo *II = ND->getIdentifier())
60           if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
61             continue;
62 
63       Importer.Import(*D);
64     }
65 
66     delete Unit;
67   }
68 
69   AdaptedAction->ExecuteAction();
70   CI.getDiagnostics().getClient()->EndSourceFile();
71 }
72 
73 void ASTMergeAction::EndSourceFileAction() {
74   return AdaptedAction->EndSourceFileAction();
75 }
76 
77 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
78                                std::string *ASTFiles, unsigned NumASTFiles)
79   : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
80   assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
81 }
82 
83 ASTMergeAction::~ASTMergeAction() {
84   delete AdaptedAction;
85 }
86 
87 bool ASTMergeAction::usesPreprocessorOnly() const {
88   return AdaptedAction->usesPreprocessorOnly();
89 }
90 
91 bool ASTMergeAction::usesCompleteTranslationUnit() {
92   return AdaptedAction->usesCompleteTranslationUnit();
93 }
94 
95 bool ASTMergeAction::hasPCHSupport() const {
96   return AdaptedAction->hasPCHSupport();
97 }
98 
99 bool ASTMergeAction::hasASTFileSupport() const {
100   return AdaptedAction->hasASTFileSupport();
101 }
102 
103 bool ASTMergeAction::hasCodeCompletionSupport() const {
104   return AdaptedAction->hasCodeCompletionSupport();
105 }
106