-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathUserInterface.cpp
More file actions
170 lines (146 loc) · 5.27 KB
/
Copy pathUserInterface.cpp
File metadata and controls
170 lines (146 loc) · 5.27 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "cling/UserInterface/UserInterface.h"
#include "cling/Interpreter/Exception.h"
#include "cling/MetaProcessor/MetaProcessor.h"
#include "cling/Utils/Output.h"
#include "textinput/Callbacks.h"
#include "textinput/TextInput.h"
#include "textinput/StreamReader.h"
#include "textinput/TerminalDisplay.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Config/config.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Frontend/CompilerInstance.h"
// Fragment copied from LLVM's raw_ostream.cpp
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif
#if defined(_MSC_VER)
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
# define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
# define STDERR_FILENO 2
#endif
#endif
#include <memory>
namespace {
///\brief Class that specialises the textinput TabCompletion to allow Cling
/// to code complete through its own textinput mechanism which is part of the
/// UserInterface.
///
class UITabCompletion : public textinput::TabCompletion {
const cling::Interpreter& m_ParentInterpreter;
public:
UITabCompletion(const cling::Interpreter& Parent) :
m_ParentInterpreter(Parent) {}
~UITabCompletion() {}
bool Complete(textinput::Text& Line /*in+out*/,
size_t& Cursor /*in+out*/,
textinput::EditorRange& R /*out*/,
std::vector<std::string>& Completions /*out*/) override {
m_ParentInterpreter.codeComplete(Line.GetText(), Cursor, Completions);
return true;
}
};
}
namespace cling {
UserInterface::UserInterface(Interpreter& interp) {
m_MetaProcessor.reset(new MetaProcessor(interp, cling::outs()));
llvm::install_fatal_error_handler(&CompilationException::throwingHandler);
}
UserInterface::~UserInterface() {}
void UserInterface::runInteractively(bool nologo /* = false */) {
if (!nologo) {
PrintLogo();
}
llvm::SmallString<512> histfilePath;
if (!getenv("CLING_NOHISTORY")) {
// History file is $HOME/.cling_history
if (llvm::sys::path::home_directory(histfilePath))
llvm::sys::path::append(histfilePath, ".cling_history");
}
using namespace textinput;
std::unique_ptr<StreamReader> R(StreamReader::Create());
std::unique_ptr<TerminalDisplay> D(TerminalDisplay::Create());
TextInput TI(*R, *D, histfilePath.empty() ? 0 : histfilePath.c_str());
// Inform text input about the code complete consumer
// TextInput owns the TabCompletion.
UITabCompletion* Completion =
new UITabCompletion(m_MetaProcessor->getInterpreter());
TI.SetCompletion(Completion);
if (D->GetTERM() && strstr(D->GetTERM(), "dumb")) {
TI.SetIsDumbTerm(true);
}
std::string Line;
std::string Prompt("[cling]$ ");
while (true) {
try {
m_MetaProcessor->getOuts().flush();
{
MetaProcessor::MaybeRedirectOutputRAII RAII(*m_MetaProcessor);
TI.SetPrompt(Prompt.c_str());
if (TI.ReadInput() == TextInput::kRREOF)
break;
TI.TakeInput(Line);
}
cling::Interpreter::CompilationResult compRes;
const int indent = m_MetaProcessor->process(Line.c_str(), compRes);
// Quit requested?
if (indent < 0)
break;
Prompt.replace(7, std::string::npos,
m_MetaProcessor->getInterpreter().isRawInputEnabled() ? "! " : "$ ");
// Continuation requested?
if (indent > 0) {
Prompt.append(1, '?');
Prompt.append(indent * 3, ' ');
}
}
catch(InterpreterException& e) {
if (!e.diagnose()) {
cling::errs() << ">>> Caught an interpreter exception!\n"
<< ">>> " << e.what() << '\n';
}
}
catch(std::exception& e) {
cling::errs() << ">>> Caught a std::exception!\n"
<< ">>> " << e.what() << '\n';
}
catch(...) {
cling::errs() << "Exception occurred. Recovering...\n";
}
}
m_MetaProcessor->getOuts().flush();
}
void UserInterface::PrintLogo() {
llvm::raw_ostream& outs = m_MetaProcessor->getOuts();
const clang::LangOptions& LangOpts
= m_MetaProcessor->getInterpreter().getCI()->getLangOpts();
if (LangOpts.CPlusPlus) {
outs << "\n"
"****************** CLING ******************\n"
"* Type C++ code and press enter to run it *\n"
"* Type .q to exit *\n"
"*******************************************\n";
} else {
outs << "\n"
"***************** CLING *****************\n"
"* Type C code and press enter to run it *\n"
"* Type .q to exit *\n"
"*****************************************\n";
}
}
} // end namespace cling