summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/sharedvisitor/generator.cxx
blob: 286d57e6d4e112fed5d12871b87372b05fd6673a (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* -*- 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/.
 */

/*
This tool generates another "plugin" which in fact only dispatches Visit* and Traverse*
calls to all other plugins registered with it. This means that there is just one
RecursiveASTVisitor pass for all those plugins instead of one per each, which
with the current number of plugins actually makes a performance difference.

If you work on a plugin, comment out LO_CLANG_SHARED_PLUGINS in Makefile-clang.mk in order
to disable the feature (re-generating takes time).

Requirements for plugins:
- Can use Visit* and Traverse* functions, but not WalkUp*.
- Visit* functions can generally remain unmodified.
- run() function must be split into preRun() and postRun() if there's any additional functionality
  besides calling TraverseDecl(). The shared visitor will call the preRun() and postRun() functions
  as necessary while calling its own run(). The run() function of the plugin must stay
  (in case of a non-shared build) but should generally look like this:
    if( preRun())
        if( TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()))
            postRun();
- Traverse* functions must be split into PreTraverse* and PostTraverse*, similarly to how run()
  is handled, the Traverse* function should generally look like this:
        bool ret = true;
        if( PreTraverse*(decl))
        {
            ret = RecursiveASTVisitor::Traverse*(decl);
            PostTraverse*(decl, ret);
        }
        return ret;


TODO:
- Create macros for the standardized layout of run(), Traverse*, etc.?
- Possibly check plugin sources more thoroughly (e.g. that run() doesn't actually do more).
- Have one tool that extracts info from plugin .cxx files into some .txt file and another tool
  that generates sharedvisitor.cxx based on those files? That would generally make the generation
  faster when doing incremental changes. The .txt file could also contain some checksum of the .cxx
  to avoid the analysing pass completely if just the timestamp has changed.
- Do not re-compile sharedvisitor.cxx if its contents have not actually changed.
- Is it possible to make the clang code analyze just the .cxx without also parsing all the headers?
- Instead of having to comment out LO_CLANG_SHARED_PLUGINS, implement --enable-compiler-plugins=debug .

*/

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"

#include <cstddef>
#include <cstring>
#include <iostream>
#include <fstream>
#include <memory>
#include <set>

#include "config_clang.h"
#include "../check.hxx"
#include "../check.cxx"

using namespace std;

using namespace clang;

using namespace loplugin;

// Info about a Visit* function in a plugin.
struct VisitFunctionInfo
{
    string name;
    string argument;
};


// Info about a Traverse* function in a plugin.
struct TraverseFunctionInfo
{
    string name;
    string argument;
    bool hasPre = false;
    bool hasPost = false;
};

struct VisitFunctionInfoLess
{
    bool operator()( const VisitFunctionInfo& l, const VisitFunctionInfo& r ) const
    {
        return l.name < r.name;
    }
};

struct TraverseFunctionInfoLess
{
    bool operator()( const TraverseFunctionInfo& l, const TraverseFunctionInfo& r ) const
    {
        return l.name < r.name;
    }
};


// Information about each LO plugin.
struct PluginInfo
{
    string className; // e.g. "BadStatics"
    string variableName; // e.g. "badStatics"
    string lowercaseName;
    bool shouldVisitTemplateInstantiations;
    bool shouldVisitImplicitCode;
    set< VisitFunctionInfo, VisitFunctionInfoLess > visitFunctions;
    set< TraverseFunctionInfo, TraverseFunctionInfoLess > traverseFunctions;
};

// We need separate visitors for shouldVisitTemplateInstantiations and shouldVisitImplicitCode,
// so split plugins into groups by what they should visit.
// It seems that trying to handle the shouldVisit* functionality with just one visitor
// is tricky.
enum PluginType
{
    PluginBasic,
    PluginVisitTemplates,
    PluginVisitImplicit,
    PluginVisitTemplatesImplicit,
};

const int Plugin_Begin = PluginBasic;
const int Plugin_End = PluginVisitTemplatesImplicit + 1;
static const char* const pluginTypeNames[ Plugin_End ]
    = { "Basic", "VisitTemplates", "VisitImplicit", "VisitTemplatesImplicit" };

static vector< PluginInfo > plugins[ Plugin_End ];


void generateVisitor( PluginType type );

void generate()
{
    ostream& output = cout;
    output <<
"// This file is autogenerated. Do not modify.\n"
"// Generated by compilerplugins/clang/sharedvisitor/generator.cxx .\n"
"\n"
"#ifdef LO_CLANG_SHARED_PLUGINS\n"
"\n"
"#include <config_clang.h>\n"
"\n"
"#include <clang/AST/ASTContext.h>\n"
"#include <clang/AST/RecursiveASTVisitor.h>\n"
"\n"
"#include \"../plugin.hxx\"\n"
"\n";

    output << "#undef LO_CLANG_SHARED_PLUGINS // to get sources of individual plugins\n";
    for( const auto& pluginGroup : plugins )
        for( const PluginInfo& plugin : pluginGroup )
            output << "#include \"../" << plugin.lowercaseName << ".cxx\"" << endl;

    output <<
"\n"
"using namespace clang;\n"
"using namespace llvm;\n"
"\n"
"namespace loplugin\n"
"{\n";

    for( int type = Plugin_Begin; type < Plugin_End; ++type )
        generateVisitor( static_cast< PluginType >( type ));

    output <<
"} // namespace loplugin\n"
"\n"
"#endif // LO_CLANG_SHARED_PLUGINS\n";
};

void generateVisitor( PluginType type )
{
    if( plugins[ type ].empty())
        return;
    ostream& output = cout;
    output <<
"\n"
"class SharedRecursiveASTVisitor" << pluginTypeNames[ type ] << "\n"
"    : public FilteringPlugin< SharedRecursiveASTVisitor" << pluginTypeNames[ type ] << ">\n"
"{\n"
"public:\n"
"    explicit SharedRecursiveASTVisitor" << pluginTypeNames[ type ] << "(const InstantiationData& rData)\n"
"        : FilteringPlugin(rData)\n";
    for( const PluginInfo& plugin : plugins[ type ] )
        output << "        , " << plugin.variableName << "( nullptr )\n";
    output << "        {}\n";

    output <<
"    virtual bool preRun() override\n"
"    {\n";
    for( const PluginInfo& plugin : plugins[ type ] )
    {
        output << "        if( " << plugin.variableName << " && !" << plugin.variableName << "->preRun())\n";
        // This will disable the plugin for the rest of the run.
        output << "            " << plugin.variableName << " = nullptr;\n";
    }
    output <<
"        return anyPluginActive();\n"
"    }\n";

    output <<
"    virtual void postRun() override\n"
"    {\n";
    for( const PluginInfo& plugin : plugins[ type ] )
    {
        output << "        if( " << plugin.variableName << " )\n";
        output << "            " << plugin.variableName << "->postRun();\n";
    }
    output <<
"    }\n";

    output <<
"    virtual void run() override {\n"
"        if (preRun()) {\n"
"            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());\n"
"            postRun();\n"
"        }\n"
"    }\n"
"    enum { isSharedPlugin = true };\n";

    output <<
"    virtual bool setSharedPlugin( Plugin* plugin, const char* name ) override\n"
"    {\n";
    bool first = true;
    for( const PluginInfo& plugin : plugins[ type ] )
    {
        output << "        ";
        if( !first )
            output << "else ";
        first = false;
        output << "if( strcmp( name, \"" << plugin.lowercaseName << "\" ) == 0 )\n";
        output << "            " << plugin.variableName << " = static_cast< " << plugin.className << "* >( plugin );\n";
    }
    output <<
"        else\n"
"            return false;\n"
"        return true;\n"
"    }\n";

    if( type == PluginVisitTemplates || type == PluginVisitTemplatesImplicit )
        output << "bool shouldVisitTemplateInstantiations() const { return true; }\n";
    if( type == PluginVisitImplicit || type == PluginVisitTemplatesImplicit )
        output << "bool shouldVisitImplicitCode() const { return true; }\n";

    set< VisitFunctionInfo, VisitFunctionInfoLess > visitFunctions;
    for( const PluginInfo& plugin : plugins[ type ] )
        for( const VisitFunctionInfo& visit : plugin.visitFunctions )
            visitFunctions.insert( visit );
    for( const VisitFunctionInfo& visit : visitFunctions )
    {
        output << "    bool " << visit.name << "(" << visit.argument << " arg)\n";
        output <<
"    {\n"
"        if( ignoreLocation( arg ))\n"
"            return true;\n";
        for( const PluginInfo& plugin : plugins[ type ] )
        {
            if( plugin.visitFunctions.find( visit ) == plugin.visitFunctions.end())
                continue;
            output << "        if( " << plugin.variableName << " != nullptr ";
            output << ")\n";
            output << "        {\n";
            output << "            if( !" << plugin.variableName << "->" << visit.name << "( arg ))\n";
            // This will disable the plugin for the rest of the run (as would returning false
            // from Visit* normally do in the non-shared case).
            output << "                " << plugin.variableName << " = nullptr;\n";
            output << "        }\n";
        }
        output <<
"        return anyPluginActive();\n"
"    }\n";
    }

    set< TraverseFunctionInfo, TraverseFunctionInfoLess > traverseFunctions;
    for( const PluginInfo& plugin : plugins[ type ] )
        for( const TraverseFunctionInfo& traverse : plugin.traverseFunctions )
            traverseFunctions.insert( traverse );
    for( const TraverseFunctionInfo& traverse : traverseFunctions )
    {
        output << "    bool " << traverse.name << "(" << traverse.argument << " arg)\n";
        output << "    {\n";
        for( const PluginInfo& plugin : plugins[ type ] )
        {
            auto pluginTraverse = plugin.traverseFunctions.find( traverse );
            if( pluginTraverse == plugin.traverseFunctions.end())
                continue;
            output << "        " << plugin.className << "* save" << plugin.className << " = " << plugin.variableName << ";\n";
            if( pluginTraverse->hasPre )
            {
                output << "        if( " << plugin.variableName << " != nullptr ";
                output << ")\n";
                output << "        {\n";
                output << "            if( !" << plugin.variableName << "->Pre" << traverse.name << "( arg ))\n";
                // This will disable the plugin for the time of the traverse, until restored later,
                // just like directly returning from Traverse* would skip that part.
                output << "                " << plugin.variableName << " = nullptr;\n";
                output << "        }\n";
            }
        }
        output << "        bool ret = RecursiveASTVisitor::" << traverse.name << "( arg );\n";
        for( const PluginInfo& plugin : plugins[ type ] )
        {
            auto pluginTraverse = plugin.traverseFunctions.find( traverse );
            if( pluginTraverse == plugin.traverseFunctions.end())
                continue;
            if( pluginTraverse->hasPost )
            {
                output << "        if( " << plugin.variableName << " != nullptr ";
                output << ")\n";
                output << "        {\n";
                output << "            if( !" << plugin.variableName << "->Post" << traverse.name << "( arg, ret ))\n";
                // This will disable the plugin for the rest of the run.
                output << "                save" << plugin.className << " = nullptr;\n";
                output << "        }\n";
            }
            output << "        " << plugin.variableName << " = save" << plugin.className << ";\n";
        }
        output << "        return ret;\n";
        output << "    }\n";
    }

    output <<
"private:\n";

    output <<
"    bool anyPluginActive() const\n"
"    {\n";
    first = true;
    for( const PluginInfo& plugin : plugins[ type ] )
    {
        if( first )
            output << "        return " << plugin.variableName << " != nullptr";
        else
            output << "\n            || " << plugin.variableName << " != nullptr";
        first = false;
    }
    output << ";\n";
    output << "    }\n";

    for( const PluginInfo& plugin : plugins[ type ] )
        output << "    " << plugin.className << "* " << plugin.variableName << ";\n";

    output <<
"};\n"
"\n"
"loplugin::Plugin::Registration< SharedRecursiveASTVisitor" << pluginTypeNames[ type ]
    << " > registration" << pluginTypeNames[ type ] << "(\"sharedvisitor" << pluginTypeNames[ type ] << "\");\n"
"\n";
}

class CheckFileVisitor
    : public RecursiveASTVisitor< CheckFileVisitor >
{
public:
    bool VisitCXXRecordDecl(CXXRecordDecl *Declaration);

    bool TraverseNamespaceDecl(NamespaceDecl * decl)
    {
        // Skip non-LO namespaces the same way FilteringPlugin does.
        if( !ContextCheck( decl ).Namespace( "loplugin" ).GlobalNamespace()
            && !ContextCheck( decl ).AnonymousNamespace())
        {
            return true;
        }
        return RecursiveASTVisitor<CheckFileVisitor>::TraverseNamespaceDecl(decl);
    }
};

static bool inheritsPluginClassCheck( const Decl* decl )
{
    return bool( DeclCheck( decl ).Class( "FilteringPlugin" ).Namespace( "loplugin" ).GlobalNamespace())
        || bool( DeclCheck( decl ).Class( "FilteringRewritePlugin" ).Namespace( "loplugin" ).GlobalNamespace());
}

static TraverseFunctionInfo findOrCreateTraverseFunctionInfo( PluginInfo& pluginInfo, StringRef name )
{
    TraverseFunctionInfo info;
    info.name = name;
    auto foundInfo = pluginInfo.traverseFunctions.find( info );
    if( foundInfo != pluginInfo.traverseFunctions.end())
    {
        info = move( *foundInfo );
        pluginInfo.traverseFunctions.erase( foundInfo );
    }
    return info;
}

static bool foundSomething;

bool CheckFileVisitor::VisitCXXRecordDecl( CXXRecordDecl* decl )
{
    if( !isDerivedFrom( decl, inheritsPluginClassCheck ))
        return true;

    if( decl->getName() == "FilteringPlugin" || decl->getName() == "FilteringRewritePlugin" )
        return true;

    PluginInfo pluginInfo;
    pluginInfo.className = decl->getName().str();
    pluginInfo.variableName = pluginInfo.className;
    assert( pluginInfo.variableName.size() > 0 );
    pluginInfo.variableName[ 0 ] = tolower( pluginInfo.variableName[ 0 ] );
    pluginInfo.lowercaseName = pluginInfo.className;
    for( char& c : pluginInfo.lowercaseName )
        c = tolower( c );
    pluginInfo.shouldVisitTemplateInstantiations = false;
    pluginInfo.shouldVisitImplicitCode = false;
    for( const CXXMethodDecl* method : decl->methods())
    {
        if( !method->getDeclName().isIdentifier())
            continue;
        if( method->isStatic() || method->getAccess() != AS_public )
            continue;
        if( method->getName().startswith( "Visit" ))
        {
            if( method->getNumParams() == 1 )
            {
                VisitFunctionInfo visitInfo;
                visitInfo.name = method->getName().str();
                visitInfo.argument = method->getParamDecl( 0 )->getTypeSourceInfo()->getType().getAsString();
                pluginInfo.visitFunctions.insert( move( visitInfo ));
            }
            else
            {
                cerr << "Unhandled Visit* function: " << pluginInfo.className << "::" << method->getName().str() << endl;
                abort();
            }
        }
        else if( method->getName().startswith( "Traverse" ))
        {
            if( method->getNumParams() == 1 )
            {
                TraverseFunctionInfo traverseInfo = findOrCreateTraverseFunctionInfo( pluginInfo, method->getName());
                traverseInfo.argument = method->getParamDecl( 0 )->getTypeSourceInfo()->getType().getAsString();
                pluginInfo.traverseFunctions.insert( move( traverseInfo ));
            }
            else
            {
                cerr << "Unhandled Traverse* function: " << pluginInfo.className << "::" << method->getName().str() << endl;
                abort();
            }
        }
        else if( method->getName().startswith( "PreTraverse" ))
        {
                TraverseFunctionInfo traverseInfo = findOrCreateTraverseFunctionInfo( pluginInfo, method->getName(). substr( 3 ));
                traverseInfo.hasPre = true;
                pluginInfo.traverseFunctions.insert( move( traverseInfo ));
        }
        else if( method->getName().startswith( "PostTraverse" ))
        {
                TraverseFunctionInfo traverseInfo = findOrCreateTraverseFunctionInfo( pluginInfo, method->getName().substr( 4 ));
                traverseInfo.hasPost = true;
                pluginInfo.traverseFunctions.insert( move( traverseInfo ));
        }
        else if( method->getName() == "shouldVisitTemplateInstantiations" )
            pluginInfo.shouldVisitTemplateInstantiations = true;
        else if( method->getName() == "shouldVisitImplicitCode" )
            pluginInfo.shouldVisitImplicitCode = true;
        else if( method->getName().startswith( "WalkUp" ))
        {
            cerr << "WalkUp function not supported for shared visitor: " << pluginInfo.className << "::" << method->getName().str() << endl;
            abort();
        }
    }

    if( pluginInfo.shouldVisitTemplateInstantiations && pluginInfo.shouldVisitImplicitCode )
        plugins[ PluginVisitTemplatesImplicit ].push_back( move( pluginInfo ));
    else if( pluginInfo.shouldVisitTemplateInstantiations )
        plugins[ PluginVisitTemplates ].push_back( move( pluginInfo ));
    else if( pluginInfo.shouldVisitImplicitCode )
        plugins[ PluginVisitImplicit ].push_back( move( pluginInfo ));
    else
        plugins[ PluginBasic ].push_back( move( pluginInfo ));

    foundSomething = true;
    return true;
}


class FindNamedClassConsumer
    : public ASTConsumer
{
public:
    virtual void HandleTranslationUnit(ASTContext& context) override
    {
        visitor.TraverseDecl( context.getTranslationUnitDecl());
    }
private:
    CheckFileVisitor visitor;
};

class FindNamedClassAction
    : public ASTFrontendAction
    {
public:
    virtual unique_ptr<ASTConsumer> CreateASTConsumer( CompilerInstance&, StringRef ) override
    {
        return unique_ptr<ASTConsumer>( new FindNamedClassConsumer );
    }
};


string readSourceFile( const char* filename )
{
    string contents;
    ifstream stream( filename );
    if( !stream )
    {
        cerr << "Failed to open: " << filename << endl;
        exit( 1 );
    }
    string line;
    bool hasIfdef = false;
    while( getline( stream, line ))
    {
        // TODO add checks that it's e.g. not "#ifdef" ?
        if( line.find( "#ifndef LO_CLANG_SHARED_PLUGINS" ) == 0 )
            hasIfdef = true;
        contents += line;
        contents += '\n';
    }
    if( stream.eof() && hasIfdef )
        return contents;
    return "";
}

int main(int argc, char** argv)
{
    vector< string > args;
    int i = 1;
    for( ; i < argc; ++ i )
    {
        constexpr std::size_t prefixlen = 5; // strlen("-arg=");
        if (std::strncmp(argv[i], "-arg=", prefixlen) != 0)
        {
            break;
        }
        args.push_back(argv[i] + prefixlen);
    }
#define STRINGIFY2(a) #a
#define STRINGIFY(a) STRINGIFY2(a)
    args.insert(
        args.end(),
        {
            "-I" BUILDDIR "/config_host", // plugin sources use e.g. config_global.h
            "-I" STRINGIFY(CLANGDIR) "/include", // clang's headers
            "-I" STRINGIFY(CLANGSYSINCLUDE), // clang system headers
            STDOPTION,
            "-D__STDC_CONSTANT_MACROS", // Clang headers require these.
            "-D__STDC_FORMAT_MACROS",
            "-D__STDC_LIMIT_MACROS",
        });
    for( ; i < argc; ++ i )
    {
        string contents = readSourceFile(argv[i]);
        if( contents.empty())
            continue;
        foundSomething = false;
#if CLANG_VERSION >= 100000
        if( !clang::tooling::runToolOnCodeWithArgs( std::unique_ptr<FindNamedClassAction>(new FindNamedClassAction), contents, args, argv[ i ] ))
#else
        if( !clang::tooling::runToolOnCodeWithArgs( new FindNamedClassAction, contents, args, argv[ i ] ))
#endif
        {
            cerr << "Failed to analyze: " << argv[ i ] << endl;
            return 2;
        }
        if( !foundSomething )
        {
            // there's #ifndef LO_CLANG_SHARED_PLUGINS in the source, but no class matched
            cerr << "Failed to find code: " << argv[ i ] << endl;
            return 2;
        }
    }
    for( int type = Plugin_Begin; type < Plugin_End; ++type )
    {
        sort( plugins[ static_cast< PluginType >( type ) ].begin(), plugins[ static_cast< PluginType >( type ) ].end(),
            []( const PluginInfo& l, const PluginInfo& r ) { return l.className < r.className; } );
    }
    generate();
    return 0;
}