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