xref: /llvm-project/clang/lib/Frontend/ASTMerge.cpp (revision 2fbe558cfe8de9e6c3fb1a8728e91df3d16d0413)
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     Diagnostic ASTDiags(CI.getDiagnostics().getClient());
41 
42     ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], ASTDiags,
43                                              false, true);
44     if (!Unit)
45       continue;
46 
47     ASTDiags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
48                               &Unit->getASTContext());
49     ASTImporter Importer(CI.getASTContext(),
50                          CI.getFileManager(),
51                          CI.getDiagnostics(),
52                          Unit->getASTContext(),
53                          Unit->getFileManager(),
54                          ASTDiags);
55 
56     TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
57     for (DeclContext::decl_iterator D = TU->decls_begin(),
58                                  DEnd = TU->decls_end();
59          D != DEnd; ++D) {
60       // FIXME: We only merge variables whose names start with x. Why
61       // would anyone want anything else?
62       if (VarDecl *VD = dyn_cast<VarDecl>(*D))
63         if (VD->getIdentifier() &&
64             *VD->getIdentifier()->getNameStart() == 'x') {
65           Decl *Merged = Importer.Import(VD);
66           (void)Merged;
67         }
68     }
69 
70     delete Unit;
71   }
72 
73   AdaptedAction->ExecuteAction();
74   CI.getDiagnostics().getClient()->EndSourceFile();
75 }
76 
77 void ASTMergeAction::EndSourceFileAction() {
78   return AdaptedAction->EndSourceFileAction();
79 }
80 
81 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
82                                std::string *ASTFiles, unsigned NumASTFiles)
83   : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
84   assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
85 }
86 
87 ASTMergeAction::~ASTMergeAction() {
88   delete AdaptedAction;
89 }
90 
91 bool ASTMergeAction::usesPreprocessorOnly() const {
92   return AdaptedAction->usesPreprocessorOnly();
93 }
94 
95 bool ASTMergeAction::usesCompleteTranslationUnit() {
96   return AdaptedAction->usesCompleteTranslationUnit();
97 }
98 
99 bool ASTMergeAction::hasPCHSupport() const {
100   return AdaptedAction->hasPCHSupport();
101 }
102 
103 bool ASTMergeAction::hasASTSupport() const {
104   return AdaptedAction->hasASTSupport();
105 }
106 
107 bool ASTMergeAction::hasCodeCompletionSupport() const {
108   return AdaptedAction->hasCodeCompletionSupport();
109 }
110