forked from mikael-s-persson/templight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplightAction.cpp
149 lines (124 loc) · 4.68 KB
/
TemplightAction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//===- TemplightAction.cpp ------ Clang Templight Frontend Action -*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TemplightAction.h"
#include "TemplightTracer.h"
#include "TemplightDebugger.h"
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/TemplateInstCallbacks.h>
#include <llvm/ADT/Twine.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/Regex.h>
#include <algorithm>
namespace clang {
std::unique_ptr<clang::ASTConsumer> TemplightAction::CreateASTConsumer(
CompilerInstance &CI, StringRef InFile) {
return WrapperFrontendAction::CreateASTConsumer(CI, InFile);
}
bool TemplightAction::BeginInvocation(CompilerInstance &CI) {
return WrapperFrontendAction::BeginInvocation(CI);
}
bool TemplightAction::BeginSourceFileAction(CompilerInstance &CI,
StringRef Filename) {
return WrapperFrontendAction::BeginSourceFileAction(CI, Filename);
}
std::string TemplightAction::CreateOutputFilename(
CompilerInstance *CI,
const std::string& OptOutputName,
bool OptInstProfiler, bool OptOutputToStdOut, bool OptMemoryProfile) {
std::string result;
if ( !OptInstProfiler )
return result; // no need for an output-filename.
if ( OptOutputToStdOut ) {
return "-";
} else if ( CI && OptOutputName.empty() ) {
result = CI->getFrontendOpts().OutputFile;
} else {
result = OptOutputName;
}
// Should never get executed.
if ( CI && result.empty() ) {
// then, derive output name from the input name:
if ( CI->hasSourceManager() ) {
FileID fileID = CI->getSourceManager().getMainFileID();
result = CI->getSourceManager().getFileEntryForID(fileID)->getName();
} else { // or, last resort:
result = "a";
}
}
if( result.rfind(".trace.") == std::string::npos ) {
result += (OptMemoryProfile ? ".memory.trace." : ".trace.");
result += "pbf";
}
return result;
}
void TemplightAction::EnsureHasSema(CompilerInstance& CI) {
if (!CI.hasSema()) {
// This part is normally done by ASTFrontEndAction, but needs to happen
// before Templight observers can be created ----------------------->>
// FIXME: Move the truncation aspect of this into Sema, we delayed this till
// here so the source manager would be initialized.
if (hasCodeCompletionSupport() &&
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
CI.createCodeCompletionConsumer();
// Use a code completion consumer?
CodeCompleteConsumer *CompletionConsumer = nullptr;
if (CI.hasCodeCompletionConsumer())
CompletionConsumer = &CI.getCodeCompletionConsumer();
CI.createSema(getTranslationUnitKind(), CompletionConsumer);
//<<--------------------------------------------------------------
}
}
void TemplightAction::ExecuteAction() {
CompilerInstance &CI = WrapperFrontendAction::getCompilerInstance();
if (!CI.hasPreprocessor())
return;
if ( InstProfiler ) {
EnsureHasSema(CI);
TemplightTracer* p_t = new TemplightTracer(CI.getSema(), OutputFilename,
MemoryProfile, OutputInSafeMode, IgnoreSystemInst);
p_t->readBlacklists(BlackListFilename);
TemplateInstantiationCallbacks::appendNewCallbacks(
CI.getSema().TemplateInstCallbacksChain, p_t);
}
if ( InteractiveDebug ) {
EnsureHasSema(CI);
TemplightDebugger* p_t = new TemplightDebugger(CI.getSema(),
MemoryProfile, IgnoreSystemInst);
p_t->readBlacklists(BlackListFilename);
TemplateInstantiationCallbacks::appendNewCallbacks(
CI.getSema().TemplateInstCallbacksChain, p_t);
}
WrapperFrontendAction::ExecuteAction();
}
void TemplightAction::EndSourceFileAction() {
WrapperFrontendAction::EndSourceFileAction();
}
bool TemplightAction::usesPreprocessorOnly() const {
return WrapperFrontendAction::usesPreprocessorOnly();
}
TranslationUnitKind TemplightAction::getTranslationUnitKind() {
return WrapperFrontendAction::getTranslationUnitKind();
}
bool TemplightAction::hasPCHSupport() const {
return WrapperFrontendAction::hasPCHSupport();
}
bool TemplightAction::hasASTFileSupport() const {
return WrapperFrontendAction::hasASTFileSupport();
}
bool TemplightAction::hasIRSupport() const {
return WrapperFrontendAction::hasIRSupport();
}
bool TemplightAction::hasCodeCompletionSupport() const {
return WrapperFrontendAction::hasCodeCompletionSupport();
}
TemplightAction::TemplightAction(std::unique_ptr<FrontendAction> WrappedAction) :
WrapperFrontendAction(std::move(WrappedAction)) {
}
}