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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// only compile this on unixy system
// as we don't want to bother with x-platform system()/mkdir()
#if defined(__unix__)
// only compile this on clang 3.7 or higher, which is known to work
// there were problems on clang 3.5 at least
#include "config_clang.h"
#include <cassert>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <regex>
#include "check.hxx"
#include "plugin.hxx"
#include "clang/Frontend/CompilerInstance.h"
namespace {
clang::Expr const * ignoreParenImplicitComma(clang::Expr const * expr) {
for (;;) {
auto const e1 = expr->IgnoreParens()->IgnoreImplicit();
if (e1 != expr) {
expr = e1;
continue;
}
// auto const e1 = dyn_cast<clang::ExprWithCleanups>(expr);
// if (e1 != nullptr) {
// expr = e1->getSubExpr();
// continue;
// }
auto const e2 = dyn_cast<clang::BinaryOperator>(expr);
if (e2 != nullptr && e2->getOpcode() == clang::BO_Comma) {
expr = e2->getRHS();
continue;
}
return expr;
}
}
bool overridesXServiceInfo(clang::CXXMethodDecl const * decl) {
for (auto i = decl->begin_overridden_methods();
i != decl->end_overridden_methods(); ++i)
{
if (((*i)->getParent()->getQualifiedNameAsString()
== "com::sun::star::lang::XServiceInfo")
|| overridesXServiceInfo(*i))
{
return true;
}
}
return false;
}
std::string replace_all(std::string subject, const std::string& search, const std::string& replace)
{
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos)
{
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
class GetImplementationName:
public loplugin::FilteringPlugin<GetImplementationName>
{
public:
explicit GetImplementationName(loplugin::InstantiationData const & data)
: FilteringPlugin(data)
, m_Outdir(initOutdir())
, m_OutdirCreated(false)
, m_Srcdir(initSrcdir())
{}
void run() override;
bool VisitCXXMethodDecl(clang::CXXMethodDecl const * decl);
private:
bool isStringConstant(Expr const * expr, clang::StringRef * string);
bool returnsStringConstant(
FunctionDecl const * decl, clang::StringRef * string);
void ensureOutdirCreated()
{
if(m_OutdirCreated)
return;
std::string cmd("mkdir -p ");
cmd += "\"" + m_Outdir + "\"";
if(system(cmd.c_str()) != 0) {
report(
clang::DiagnosticsEngine::Error,
"Error creating ServiceImplementations output dir \"%0\".")
<< m_Outdir;
}
m_OutdirCreated = true;
}
void generateOutput(FunctionDecl const * decl, const std::string unoimpl, const std::string cppclass);
std::string initOutdir();
std::string initSrcdir();
const std::string m_Outdir;
bool m_OutdirCreated;
const std::string m_Srcdir;
};
void GetImplementationName::run() {
if (compiler.getLangOpts().CPlusPlus) {
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
}
bool GetImplementationName::VisitCXXMethodDecl(
clang::CXXMethodDecl const * decl)
{
if (ignoreLocation(decl) || !decl->doesThisDeclarationHaveABody()
|| !decl->isVirtual())
{
return true;
}
auto const id = decl->getIdentifier();
if (id == nullptr || id->getName() != "getImplementationName"
|| !overridesXServiceInfo(decl))
{
return true;
}
clang::StringRef unoimpl;
if (!returnsStringConstant(decl, &unoimpl)) {
report(
clang::DiagnosticsEngine::Warning,
"cannot determine returned string", decl->getLocation())
<< decl->getSourceRange();
return true;
}
generateOutput(decl, unoimpl.str(), decl->getParent()->getQualifiedNameAsString());
return true;
}
bool GetImplementationName::isStringConstant(
Expr const * expr, clang::StringRef * string)
{
QualType t = expr->getType();
if (!(t->isConstantArrayType() && t.isConstQualified()
&& (loplugin::TypeCheck(t->getAsArrayTypeUnsafe()->getElementType())
.Char())))
{
return false;
}
DeclRefExpr const * dre = dyn_cast<DeclRefExpr>(expr);
if (dre != nullptr) {
VarDecl const * var = dyn_cast<VarDecl>(dre->getDecl());
if (var != nullptr) {
Expr const * init = var->getAnyInitializer();
if (init != nullptr) {
expr = ignoreParenImplicitComma(init);
}
}
}
clang::StringLiteral const * lit = dyn_cast<clang::StringLiteral>(expr);
if (lit != nullptr) {
if (!lit->isAscii()) {
return false;
}
*string = lit->getString();
return true;
}
APValue v;
if (!expr->isCXX11ConstantExpr(compiler.getASTContext(), &v)) {
return false;
}
switch (v.getKind()) {
case APValue::LValue:
return false; //TODO
case APValue::Array:
{
if (v.hasArrayFiller()) { //TODO: handle final NUL filler?
return false;
}
unsigned n = v.getArraySize();
assert(n != 0);
for (unsigned i = 0; i != n; ++i) {
APValue e(v.getArrayInitializedElt(i));
if (!e.isInt()) { //TODO: assert?
return false;
}
APSInt iv = e.getInt();
if (iv == 0) {
if (i == n -1) {
continue;
}
return false;
} else if (iv.uge(0x80)) {
return false;
}
//TODO
}
return false;//TODO
}
default:
abort(); //TODO???
}
}
bool GetImplementationName::returnsStringConstant(
FunctionDecl const * decl, clang::StringRef * string)
{
auto s1 = decl->getBody();
if (s1 == nullptr) {
return false;
}
for (;;) {
auto s2 = dyn_cast<clang::CompoundStmt>(s1);
if (s2 == nullptr) {
auto const s3 = dyn_cast<clang::CXXTryStmt>(s1);
if (s3 == nullptr) {
break;
}
s2 = s3->getTryBlock();
}
if (s2->size() != 1) {
break;
}
s1 = s2->body_front();
}
auto const s4 = dyn_cast<clang::ReturnStmt>(s1);
if (s4 == nullptr) {
return false;
}
for (auto e1 = ignoreParenImplicitComma(s4->getRetValue());;) {
auto const e2 = dyn_cast<clang::CallExpr>(e1);
if (e2 != nullptr) {
auto const d = e2->getDirectCallee();
return d != nullptr && returnsStringConstant(d, string);
}
auto const e3 = dyn_cast<clang::CXXFunctionalCastExpr>(e1);
if (e3 != nullptr) {
e1 = ignoreParenImplicitComma(e3->getSubExpr());
continue;
}
auto const e4 = dyn_cast<clang::CXXConstructExpr>(e1);
if (e4 != nullptr) {
if (e4->getNumArgs() < 1) {
return false;
}
e1 = ignoreParenImplicitComma(e4->getArg(0));
continue;
}
return isStringConstant(e1, string);
}
}
void GetImplementationName::generateOutput(FunctionDecl const * decl, const std::string unoimpl, const std::string cppclass) {
ensureOutdirCreated();
clang::SourceManager& sm(compiler.getSourceManager());
const std::string absfilename(sm.getFilename(decl->getSourceRange().getBegin()).str());
if(absfilename.length() <= m_Srcdir.length())
return;
const std::string filename(absfilename.substr(m_Srcdir.length()+1));
const std::regex moduleregex("^\\w+");
std::smatch modulematch;
std::regex_search(filename, modulematch, moduleregex);
if(modulematch.empty())
return;
const std::string module(modulematch[0]);
const std::string doublecolonregex("::");
const std::string cppclassweb(replace_all(cppclass, doublecolonregex, "_1_1"));
std::ofstream redirectfile(m_Outdir + "/" + unoimpl + ".html");
redirectfile << "<meta http-equiv=\"refresh\" content=\"0; URL=http://docs.libreoffice.org/" << module << "/html/class" << cppclassweb << "\">\n";
redirectfile.close();
}
std::string GetImplementationName::initOutdir() {
{
char* pWorkdir = getenv("WORKDIR");
if(pWorkdir) {
std::string result(pWorkdir);
result += "/ServiceImplementations";
return result;
}
report(
clang::DiagnosticsEngine::Error, "WORKDIR unset, don't know where to write service implementation info.");
return std::string();
}
}
std::string GetImplementationName::initSrcdir() {
{
char* pSrcdir = getenv("SRCDIR");
if(!pSrcdir) {
report(
clang::DiagnosticsEngine::Error, "SRCDIR unset, don't know where the source base is.");
}
return std::string(pSrcdir);
}
}
loplugin::Plugin::Registration<GetImplementationName> X(
"getimplementationname", false);
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|