mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-23 13:35:42 -04:00

into another AST, including their include history. Here's an example error that involves a conflict merging a variable with different types in two translation units (diagnosed in the third AST context into which everything is merged). /Volumes/Data/dgregor/Projects/llvm/tools/clang/test/ASTMerge/Inputs/var2.c:3:5: error: external variable 'x2' declared with incompatible types in different translation units ('int' vs. 'double') int x2; ^ In file included from /Volumes/Data/dgregor/Projects/llvm/tools/clang/test/ASTMerge/Inputs/var1.c:3: /Volumes/Data/dgregor/Projects/llvm/tools/clang/test/ASTMerge/Inputs/var1.h:1:8: note: declared here with type 'double' double x2; ^ Although we maintain include history, we do not maintain macro instantiation history across a merge. Instead, we map down to the spelling location (for now!). llvm-svn: 95732
108 lines
3.7 KiB
C++
108 lines
3.7 KiB
C++
//===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/AST/ASTDiagnostic.h"
|
|
#include "clang/AST/ASTImporter.h"
|
|
|
|
using namespace clang;
|
|
|
|
ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
|
|
llvm::StringRef InFile) {
|
|
return AdaptedAction->CreateASTConsumer(CI, InFile);
|
|
}
|
|
|
|
bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
|
|
llvm::StringRef Filename) {
|
|
// FIXME: This is a hack. We need a better way to communicate the
|
|
// AST file, compiler instance, and file name than member variables
|
|
// of FrontendAction.
|
|
AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit());
|
|
AdaptedAction->setCompilerInstance(&CI);
|
|
return AdaptedAction->BeginSourceFileAction(CI, Filename);
|
|
}
|
|
|
|
void ASTMergeAction::ExecuteAction() {
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
|
|
&CI.getASTContext());
|
|
for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
|
|
Diagnostic ASTDiags(CI.getDiagnostics().getClient());
|
|
|
|
ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], ASTDiags,
|
|
false, true);
|
|
if (!Unit)
|
|
continue;
|
|
|
|
ASTDiags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
|
|
&Unit->getASTContext());
|
|
ASTImporter Importer(CI.getASTContext(),
|
|
CI.getFileManager(),
|
|
CI.getDiagnostics(),
|
|
Unit->getASTContext(),
|
|
Unit->getFileManager(),
|
|
ASTDiags);
|
|
|
|
TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
|
|
for (DeclContext::decl_iterator D = TU->decls_begin(),
|
|
DEnd = TU->decls_end();
|
|
D != DEnd; ++D) {
|
|
// FIXME: We only merge variables whose names start with x. Why
|
|
// would anyone want anything else?
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(*D))
|
|
if (VD->getIdentifier() &&
|
|
*VD->getIdentifier()->getNameStart() == 'x') {
|
|
Decl *Merged = Importer.Import(VD);
|
|
(void)Merged;
|
|
}
|
|
}
|
|
|
|
delete Unit;
|
|
}
|
|
|
|
|
|
return AdaptedAction->ExecuteAction();
|
|
}
|
|
|
|
void ASTMergeAction::EndSourceFileAction() {
|
|
return AdaptedAction->EndSourceFileAction();
|
|
}
|
|
|
|
ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
|
|
std::string *ASTFiles, unsigned NumASTFiles)
|
|
: AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
|
|
assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
|
|
}
|
|
|
|
ASTMergeAction::~ASTMergeAction() {
|
|
delete AdaptedAction;
|
|
}
|
|
|
|
bool ASTMergeAction::usesPreprocessorOnly() const {
|
|
return AdaptedAction->usesPreprocessorOnly();
|
|
}
|
|
|
|
bool ASTMergeAction::usesCompleteTranslationUnit() {
|
|
return AdaptedAction->usesCompleteTranslationUnit();
|
|
}
|
|
|
|
bool ASTMergeAction::hasPCHSupport() const {
|
|
return AdaptedAction->hasPCHSupport();
|
|
}
|
|
|
|
bool ASTMergeAction::hasASTSupport() const {
|
|
return AdaptedAction->hasASTSupport();
|
|
}
|
|
|
|
bool ASTMergeAction::hasCodeCompletionSupport() const {
|
|
return AdaptedAction->hasCodeCompletionSupport();
|
|
}
|