Implement -working-directory.
[clang.git] / lib / Frontend / ASTMerge.cpp
blobf2708463430533dbdc610a241535d190426afa7d
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 #include "clang/Basic/Diagnostic.h"
17 using namespace clang;
19 ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
20 llvm::StringRef InFile) {
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
25 llvm::StringRef Filename) {
26 // FIXME: This is a hack. We need a better way to communicate the
27 // AST file, compiler instance, and file name than member variables
28 // of FrontendAction.
29 AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30 takeCurrentASTUnit());
31 AdaptedAction->setCompilerInstance(&CI);
32 return AdaptedAction->BeginSourceFileAction(CI, Filename);
35 void ASTMergeAction::ExecuteAction() {
36 CompilerInstance &CI = getCompilerInstance();
37 CI.getDiagnostics().getClient()->BeginSourceFile(
38 CI.getASTContext().getLangOptions());
39 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40 &CI.getASTContext());
41 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
42 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
43 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
44 CI.getFileSystemOpts(), false);
45 if (!Unit)
46 continue;
48 // Reset the argument -> string function so that it has the AST
49 // context we want, since the Sema object created by
50 // LoadFromASTFile will override it.
51 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
52 &CI.getASTContext());
54 ASTImporter Importer(CI.getDiagnostics(),
55 CI.getASTContext(),
56 CI.getFileManager(),
57 CI.getFileSystemOpts(),
58 Unit->getASTContext(),
59 Unit->getFileManager(),
60 Unit->getFileSystemOpts());
62 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
63 for (DeclContext::decl_iterator D = TU->decls_begin(),
64 DEnd = TU->decls_end();
65 D != DEnd; ++D) {
66 // Don't re-import __va_list_tag, __builtin_va_list.
67 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
68 if (IdentifierInfo *II = ND->getIdentifier())
69 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
70 continue;
72 Importer.Import(*D);
75 delete Unit;
78 AdaptedAction->ExecuteAction();
79 CI.getDiagnostics().getClient()->EndSourceFile();
82 void ASTMergeAction::EndSourceFileAction() {
83 return AdaptedAction->EndSourceFileAction();
86 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
87 std::string *ASTFiles, unsigned NumASTFiles)
88 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
89 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
92 ASTMergeAction::~ASTMergeAction() {
93 delete AdaptedAction;
96 bool ASTMergeAction::usesPreprocessorOnly() const {
97 return AdaptedAction->usesPreprocessorOnly();
100 bool ASTMergeAction::usesCompleteTranslationUnit() {
101 return AdaptedAction->usesCompleteTranslationUnit();
104 bool ASTMergeAction::hasPCHSupport() const {
105 return AdaptedAction->hasPCHSupport();
108 bool ASTMergeAction::hasASTFileSupport() const {
109 return AdaptedAction->hasASTFileSupport();
112 bool ASTMergeAction::hasCodeCompletionSupport() const {
113 return AdaptedAction->hasCodeCompletionSupport();