Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/ASTMerge.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
#include "clang/Frontend/ASTUnit.h"
9
#include "clang/AST/ASTContext.h"
10
#include "clang/AST/ASTDiagnostic.h"
11
#include "clang/AST/ASTImporter.h"
12
#include "clang/AST/ASTImporterSharedState.h"
13
#include "clang/Basic/Diagnostic.h"
14
#include "clang/Frontend/CompilerInstance.h"
15
#include "clang/Frontend/FrontendActions.h"
16
17
using namespace clang;
18
19
std::unique_ptr<ASTConsumer>
20
33
ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
21
33
  return AdaptedAction->CreateASTConsumer(CI, InFile);
22
33
}
23
24
33
bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
25
  // FIXME: This is a hack. We need a better way to communicate the
26
  // AST file, compiler instance, and file name than member variables
27
  // of FrontendAction.
28
33
  AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
29
33
  AdaptedAction->setCompilerInstance(&CI);
30
33
  return AdaptedAction->BeginSourceFileAction(CI);
31
33
}
32
33
33
void ASTMergeAction::ExecuteAction() {
34
33
  CompilerInstance &CI = getCompilerInstance();
35
33
  CI.getDiagnostics().getClient()->BeginSourceFile(
36
33
                                             CI.getASTContext().getLangOpts());
37
33
  CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38
33
                                       &CI.getASTContext());
39
33
  IntrusiveRefCntPtr<DiagnosticIDs>
40
33
      DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41
33
  auto SharedState = std::make_shared<ASTImporterSharedState>(
42
33
      *CI.getASTContext().getTranslationUnitDecl());
43
85
  for (unsigned I = 0, N = ASTFiles.size(); I != N; 
++I52
) {
44
52
    IntrusiveRefCntPtr<DiagnosticsEngine>
45
52
        Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
46
52
                                    new ForwardingDiagnosticConsumer(
47
52
                                          *CI.getDiagnostics().getClient()),
48
52
                                    /*ShouldOwnClient=*/true));
49
52
    std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
50
52
        ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
51
52
        CI.getFileSystemOpts(), false);
52
53
52
    if (!Unit)
54
0
      continue;
55
56
52
    ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57
52
                         Unit->getASTContext(), Unit->getFileManager(),
58
52
                         /*MinimalImport=*/false, SharedState);
59
60
52
    TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
61
764
    for (auto *D : TU->decls()) {
62
      // Don't re-import __va_list_tag, __builtin_va_list.
63
764
      if (const auto *ND = dyn_cast<NamedDecl>(D))
64
759
        if (IdentifierInfo *II = ND->getIdentifier())
65
739
          if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
66
52
            continue;
67
68
712
      llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
69
70
712
      if (ToDOrError) {
71
653
        DeclGroupRef DGR(*ToDOrError);
72
653
        CI.getASTConsumer().HandleTopLevelDecl(DGR);
73
653
      } else {
74
59
        llvm::consumeError(ToDOrError.takeError());
75
59
      }
76
712
    }
77
52
  }
78
79
33
  AdaptedAction->ExecuteAction();
80
33
  CI.getDiagnostics().getClient()->EndSourceFile();
81
33
}
82
83
33
void ASTMergeAction::EndSourceFileAction() {
84
33
  return AdaptedAction->EndSourceFileAction();
85
33
}
86
87
ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
88
                               ArrayRef<std::string> ASTFiles)
89
33
: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
90
33
  assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
91
33
}
92
93
33
ASTMergeAction::~ASTMergeAction() {
94
33
}
95
96
33
bool ASTMergeAction::usesPreprocessorOnly() const {
97
33
  return AdaptedAction->usesPreprocessorOnly();
98
33
}
99
100
33
TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
101
33
  return AdaptedAction->getTranslationUnitKind();
102
33
}
103
104
1
bool ASTMergeAction::hasPCHSupport() const {
105
1
  return AdaptedAction->hasPCHSupport();
106
1
}
107
108
0
bool ASTMergeAction::hasASTFileSupport() const {
109
0
  return AdaptedAction->hasASTFileSupport();
110
0
}
111
112
0
bool ASTMergeAction::hasCodeCompletionSupport() const {
113
0
  return AdaptedAction->hasCodeCompletionSupport();
114
0
}