ASTMerge.cpp revision 5f9e272e632e951b1efe824cd16acb4d96077930
1c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
2c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//
3c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//                     The LLVM Compiler Infrastructure
4c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//
5c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// This file is distributed under the University of Illinois Open Source
6c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall// License. See LICENSE.TXT for details.
7c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//
8c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall//===----------------------------------------------------------------------===//
9c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/Frontend/ASTUnit.h"
10c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/Frontend/CompilerInstance.h"
11c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/Frontend/FrontendActions.h"
12c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/AST/ASTContext.h"
13c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/AST/ASTDiagnostic.h"
14c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/AST/ASTImporter.h"
15c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall#include "clang/Basic/Diagnostic.h"
16c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
17c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallusing namespace clang;
18c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
19c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
20c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                                               StringRef InFile) {
21c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return AdaptedAction->CreateASTConsumer(CI, InFile);
22c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall}
23c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall
24c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallbool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
25c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                                           StringRef Filename) {
266b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  // FIXME: This is a hack. We need a better way to communicate the
276b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  // AST file, compiler instance, and file name than member variables
28c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  // of FrontendAction.
29c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
306b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall                                takeCurrentASTUnit());
316b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  AdaptedAction->setCompilerInstance(&CI);
32c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return AdaptedAction->BeginSourceFileAction(CI, Filename);
33c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall}
345357b615364c17ea024c757354c58ae2a520d216John McCall
355357b615364c17ea024c757354c58ae2a520d216John McCallvoid ASTMergeAction::ExecuteAction() {
365357b615364c17ea024c757354c58ae2a520d216John McCall  CompilerInstance &CI = getCompilerInstance();
375357b615364c17ea024c757354c58ae2a520d216John McCall  CI.getDiagnostics().getClient()->BeginSourceFile(
385357b615364c17ea024c757354c58ae2a520d216John McCall                                         CI.getASTContext().getLangOptions());
395357b615364c17ea024c757354c58ae2a520d216John McCall  CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
405357b615364c17ea024c757354c58ae2a520d216John McCall                                       &CI.getASTContext());
4141d8903731782ee85ee2b19734008b006e01c76fJohn McCall  llvm::IntrusiveRefCntPtr<DiagnosticIDs>
42233a6419097ed97b67ff8efcacef9af613262ca3John McCall      DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
43233a6419097ed97b67ff8efcacef9af613262ca3John McCall  for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
445357b615364c17ea024c757354c58ae2a520d216John McCall    llvm::IntrusiveRefCntPtr<Diagnostic>
455357b615364c17ea024c757354c58ae2a520d216John McCall        Diags(new Diagnostic(DiagIDs, CI.getDiagnostics().getClient(),
465357b615364c17ea024c757354c58ae2a520d216John McCall                             /*ShouldOwnClient=*/false));
4741d8903731782ee85ee2b19734008b006e01c76fJohn McCall    ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
48233a6419097ed97b67ff8efcacef9af613262ca3John McCall                                             CI.getFileSystemOpts(), false);
49233a6419097ed97b67ff8efcacef9af613262ca3John McCall    if (!Unit)
505357b615364c17ea024c757354c58ae2a520d216John McCall      continue;
515357b615364c17ea024c757354c58ae2a520d216John McCall
525357b615364c17ea024c757354c58ae2a520d216John McCall    ASTImporter Importer(CI.getASTContext(),
5341d8903731782ee85ee2b19734008b006e01c76fJohn McCall                         CI.getFileManager(),
545357b615364c17ea024c757354c58ae2a520d216John McCall                         Unit->getASTContext(),
55233a6419097ed97b67ff8efcacef9af613262ca3John McCall                         Unit->getFileManager(),
56233a6419097ed97b67ff8efcacef9af613262ca3John McCall                         /*MinimalImport=*/false);
575357b615364c17ea024c757354c58ae2a520d216John McCall
585357b615364c17ea024c757354c58ae2a520d216John McCall    TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
595357b615364c17ea024c757354c58ae2a520d216John McCall    for (DeclContext::decl_iterator D = TU->decls_begin(),
605357b615364c17ea024c757354c58ae2a520d216John McCall                                 DEnd = TU->decls_end();
615357b615364c17ea024c757354c58ae2a520d216John McCall         D != DEnd; ++D) {
625357b615364c17ea024c757354c58ae2a520d216John McCall      // Don't re-import __va_list_tag, __builtin_va_list.
635357b615364c17ea024c757354c58ae2a520d216John McCall      if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
645357b615364c17ea024c757354c58ae2a520d216John McCall        if (IdentifierInfo *II = ND->getIdentifier())
656b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall          if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
666b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall            continue;
675357b615364c17ea024c757354c58ae2a520d216John McCall
686b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall      Importer.Import(*D);
696b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    }
7041d8903731782ee85ee2b19734008b006e01c76fJohn McCall
716b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall    delete Unit;
726b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall  }
736b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall
745357b615364c17ea024c757354c58ae2a520d216John McCall  AdaptedAction->ExecuteAction();
755357b615364c17ea024c757354c58ae2a520d216John McCall  CI.getDiagnostics().getClient()->EndSourceFile();
765357b615364c17ea024c757354c58ae2a520d216John McCall}
775357b615364c17ea024c757354c58ae2a520d216John McCall
785357b615364c17ea024c757354c58ae2a520d216John McCallvoid ASTMergeAction::EndSourceFileAction() {
795357b615364c17ea024c757354c58ae2a520d216John McCall  return AdaptedAction->EndSourceFileAction();
80233a6419097ed97b67ff8efcacef9af613262ca3John McCall}
81233a6419097ed97b67ff8efcacef9af613262ca3John McCall
826b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCallASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
836b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCall                               std::string *ASTFiles, unsigned NumASTFiles)
845357b615364c17ea024c757354c58ae2a520d216John McCall  : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
855357b615364c17ea024c757354c58ae2a520d216John McCall  assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
864f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
874f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
884f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallASTMergeAction::~ASTMergeAction() {
894f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  delete AdaptedAction;
904f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
914f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
924f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallbool ASTMergeAction::usesPreprocessorOnly() const {
934f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  return AdaptedAction->usesPreprocessorOnly();
944f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
954f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
966b2accb4793e16b2e93a8c2589f5df702231f17aJohn McCallbool ASTMergeAction::usesCompleteTranslationUnit() {
974f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  return AdaptedAction->usesCompleteTranslationUnit();
984f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
994f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
1004f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallbool ASTMergeAction::hasPCHSupport() const {
1014f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  return AdaptedAction->hasPCHSupport();
10258e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall}
1034f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
1044f9506a27cb6b865bf38beea48eadfa9dc93f510John McCallbool ASTMergeAction::hasASTFileSupport() const {
1054f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  return AdaptedAction->hasASTFileSupport();
1064f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
1074f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall
10858e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCallbool ASTMergeAction::hasCodeCompletionSupport() const {
1094f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall  return AdaptedAction->hasCodeCompletionSupport();
1104f9506a27cb6b865bf38beea48eadfa9dc93f510John McCall}
11158e6f34e4d2c668562e1c391162ee9de7b05fbb2John McCall