xref: /llvm-project/clang/lib/Frontend/ASTMerge.cpp (revision d03e823fb43be47f8fe3c651bfcf8a3254c3fe1c)
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 
16 using namespace clang;
17 
18 ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
19                                                llvm::StringRef InFile) {
20   return AdaptedAction->CreateASTConsumer(CI, InFile);
21 }
22 
23 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
24                                            llvm::StringRef Filename) {
25   // FIXME: This is a hack. We need a better way to communicate the
26   // AST file, compiler instance, and file name than member variables
27   // of FrontendAction.
28   AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit());
29   AdaptedAction->setCompilerInstance(&CI);
30   return AdaptedAction->BeginSourceFileAction(CI, Filename);
31 }
32 
33 void ASTMergeAction::ExecuteAction() {
34   CompilerInstance &CI = getCompilerInstance();
35   CI.getDiagnostics().getClient()->BeginSourceFile(
36                                          CI.getASTContext().getLangOptions());
37   CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38                                        &CI.getASTContext());
39   for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
40     ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I],
41                                              UnownedDiag(CI.getDiagnostics()),
42                                              false);
43     if (!Unit)
44       continue;
45 
46     ASTImporter Importer(CI.getDiagnostics(),
47                          CI.getASTContext(),
48                          CI.getFileManager(),
49                          Unit->getASTContext(),
50                          Unit->getFileManager());
51 
52     TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
53     for (DeclContext::decl_iterator D = TU->decls_begin(),
54                                  DEnd = TU->decls_end();
55          D != DEnd; ++D) {
56       // Don't re-import __va_list_tag, __builtin_va_list.
57       if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
58         if (IdentifierInfo *II = ND->getIdentifier())
59           if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
60             continue;
61 
62       Importer.Import(*D);
63     }
64 
65     delete Unit;
66   }
67 
68   AdaptedAction->ExecuteAction();
69   CI.getDiagnostics().getClient()->EndSourceFile();
70 }
71 
72 void ASTMergeAction::EndSourceFileAction() {
73   return AdaptedAction->EndSourceFileAction();
74 }
75 
76 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
77                                std::string *ASTFiles, unsigned NumASTFiles)
78   : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
79   assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
80 }
81 
82 ASTMergeAction::~ASTMergeAction() {
83   delete AdaptedAction;
84 }
85 
86 bool ASTMergeAction::usesPreprocessorOnly() const {
87   return AdaptedAction->usesPreprocessorOnly();
88 }
89 
90 bool ASTMergeAction::usesCompleteTranslationUnit() {
91   return AdaptedAction->usesCompleteTranslationUnit();
92 }
93 
94 bool ASTMergeAction::hasPCHSupport() const {
95   return AdaptedAction->hasPCHSupport();
96 }
97 
98 bool ASTMergeAction::hasASTSupport() const {
99   return AdaptedAction->hasASTSupport();
100 }
101 
102 bool ASTMergeAction::hasCodeCompletionSupport() const {
103   return AdaptedAction->hasCodeCompletionSupport();
104 }
105