ASTMerge.cpp revision 9bed8798964d9f07599c2c9199701f86fbc70e20
19bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
29bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//
39bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//                     The LLVM Compiler Infrastructure
49bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//
59bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor// This file is distributed under the University of Illinois Open Source
69bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor// License. See LICENSE.TXT for details.
79bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//
89bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor//===----------------------------------------------------------------------===//
99bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include "clang/Frontend/ASTUnit.h"
109bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include "clang/Frontend/CompilerInstance.h"
119bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include "clang/Frontend/FrontendActions.h"
129bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include "clang/AST/ASTContext.h"
139bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor#include "clang/AST/ASTImporter.h"
149bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
159bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorusing namespace clang;
169bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
179bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
189bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                                               llvm::StringRef InFile) {
199bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->CreateASTConsumer(CI, InFile);
209bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
219bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
229bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
239bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                                           llvm::StringRef Filename) {
249bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // FIXME: This is a hack. We need a better way to communicate the
259bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // AST file, compiler instance, and file name than member variables
269bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  // of FrontendAction.
279bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit());
289bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  AdaptedAction->setCompilerInstance(&CI);
299bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->BeginSourceFileAction(CI, Filename);
309bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
319bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
329bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorvoid ASTMergeAction::ExecuteAction() {
339bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  CompilerInstance &CI = getCompilerInstance();
349bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
359bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
369bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], CI.getDiagnostics(),
379bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                                             false, true);
389bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    if (!Unit)
399bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor      continue;
409bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
419bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    ASTImporter Importer(CI.getASTContext(), CI.getDiagnostics(),
429bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                         Unit->getASTContext(), CI.getDiagnostics());
439bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
449bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
459bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    for (DeclContext::decl_iterator D = TU->decls_begin(),
469bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                                 DEnd = TU->decls_end();
479bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor         D != DEnd; ++D) {
489bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor      // FIXME: We only merge variables whose names start with x. Why
499bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor      // would anyone want anything else?
509bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor      if (VarDecl *VD = dyn_cast<VarDecl>(*D))
519bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor        if (VD->getIdentifier() &&
529bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor            *VD->getIdentifier()->getNameStart() == 'x') {
539bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor          Decl *Merged = Importer.Import(VD);
549bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor          if (Merged)
559bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor            Merged->dump();
569bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor        }
579bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    }
589bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
599bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor    delete Unit;
609bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  }
619bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
629bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
639bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->ExecuteAction();
649bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
659bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
669bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorvoid ASTMergeAction::EndSourceFileAction() {
679bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->EndSourceFileAction();
689bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
699bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
709bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
719bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor                               std::string *ASTFiles, unsigned NumASTFiles)
729bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
739bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
749bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
759bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
769bed8798964d9f07599c2c9199701f86fbc70e20Douglas GregorASTMergeAction::~ASTMergeAction() {
779bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  delete AdaptedAction;
789bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
799bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
809bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::usesPreprocessorOnly() const {
819bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->usesPreprocessorOnly();
829bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
839bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
849bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::usesCompleteTranslationUnit() {
859bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->usesCompleteTranslationUnit();
869bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
879bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
889bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::hasPCHSupport() const {
899bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->hasPCHSupport();
909bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
919bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
929bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::hasASTSupport() const {
939bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->hasASTSupport();
949bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
959bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor
969bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregorbool ASTMergeAction::hasCodeCompletionSupport() const {
979bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor  return AdaptedAction->hasCodeCompletionSupport();
989bed8798964d9f07599c2c9199701f86fbc70e20Douglas Gregor}
99