summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/plugin.cxx
blob: 696118c1217427741645c601679ef28a718ccb27 (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
/*
 * This file is part of the LibreOffice project.
 *
 * Based on LLVM/Clang.
 *
 * This file is distributed under the University of Illinois Open Source
 * License. See LICENSE.TXT for details.
 *
 */

#include "plugin.hxx"

#include <clang/AST/ASTConsumer.h>
#include <clang/AST/ASTContext.h>
#include <clang/Basic/FileManager.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/FrontendPluginRegistry.h>
#include <stdio.h>

#include "bodynotinblock.hxx"
#include "lclstaticfix.hxx"
#include "postfixincrementfix.hxx"
#include "sallogareas.hxx"
#include "unusedvariablecheck.hxx"

namespace loplugin
{

Plugin::Plugin( ASTContext& context )
    : context( context )
    {
    }

DiagnosticBuilder Plugin::report( DiagnosticsEngine::Level level, StringRef message, SourceLocation loc )
    {
    DiagnosticsEngine& diag = context.getDiagnostics();
#if 0
    // Do some mappings (e.g. for -Werror) that clang does not do for custom messages for some reason.
    if( level == DiagnosticsEngine::Warning && diag.getWarningsAsErrors())
        level = DiagnosticsEngine::Error;
    if( level == DiagnosticsEngine::Error && diag.getErrorsAsFatal())
        level = DiagnosticsEngine::Fatal;
#endif
    if( loc.isValid())
        return diag.Report( loc, diag.getCustomDiagID( level, message ));
    else
        return diag.Report( diag.getCustomDiagID( level, message ));
    }

bool Plugin::ignoreLocation( SourceLocation loc )
    {
    return context.getSourceManager().isInSystemHeader( context.getSourceManager().getExpansionLoc( loc ));
    }


RewritePlugin::RewritePlugin( ASTContext& context, Rewriter& rewriter )
    : Plugin( context )
    , rewriter( rewriter )
    {
    }

bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAfter, bool indentNewLines )
    {
    if( rewriter.InsertText( Loc, Str, InsertAfter, indentNewLines ))
        return reportEditFailure( Loc );
    return true;
    }

bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
    {
    if( rewriter.InsertTextAfter( Loc, Str ))
        return reportEditFailure( Loc );
    return true;
    }

bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
    {
    if( rewriter.InsertTextAfterToken( Loc, Str ))
        return reportEditFailure( Loc );
    return true;
    }

bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
    {
    if( rewriter.InsertTextBefore( Loc, Str ))
        return reportEditFailure( Loc );
    return true;
    }

bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOptions opts )
    {
    if( rewriter.RemoveText( Start, Length, opts ))
        return reportEditFailure( Start );
    return true;
    }

bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts )
    {
    if( rewriter.RemoveText( range, opts ))
        return reportEditFailure( range.getBegin());
    return true;
    }

bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
    {
    if( rewriter.RemoveText( range, opts ))
        return reportEditFailure( range.getBegin());
    return true;
    }

bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
    {
    if( rewriter.ReplaceText( Start, OrigLength, NewStr ))
        return reportEditFailure( Start );
    return true;
    }

bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
    {
    if( rewriter.ReplaceText( range, NewStr ))
        return reportEditFailure( range.getBegin());
    return true;
    }

bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
    {
    if( rewriter.ReplaceText( range, replacementRange ))
        return reportEditFailure( range.getBegin());
    return true;
    }

bool RewritePlugin::reportEditFailure( SourceLocation loc )
    {
    DiagnosticsEngine& diag = context.getDiagnostics();
    diag.Report( loc, diag.getCustomDiagID( DiagnosticsEngine::Warning,
        "cannot perform source modification (macro expansion involved?) [loplugin]" ));
    return false;
    }


/**
 Class that manages all LO modules.
*/
class PluginHandler
    : public ASTConsumer
    {
    public:
        explicit PluginHandler( ASTContext& context, const vector< string >& args )
            : rewriter( context.getSourceManager(), context.getLangOpts())
            , args( args )
            , bodyNotInBlock( context )
            , lclStaticFix( context, rewriter )
            , postfixIncrementFix( context, rewriter )
            , salLogAreas( context )
            , unusedVariableCheck( context )
            {
            }
        virtual void HandleTranslationUnit( ASTContext& context )
            {
            if( context.getDiagnostics().hasErrorOccurred())
                return;
            if( isArg( "lclstaticfix" ))
                lclStaticFix.run();
            else if( isArg( "postfixincrementfix" ))
                postfixIncrementFix.run();
            else if( args.empty())
                {
                bodyNotInBlock.run();
                salLogAreas.run();
                unusedVariableCheck.run();
                }
            else
                {
                DiagnosticsEngine& diag = context.getDiagnostics();
                diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Fatal,
                    "unknown plugin tool %0 [loplugin]" )) << args.front();
                }
            for( Rewriter::buffer_iterator it = rewriter.buffer_begin();
                 it != rewriter.buffer_end();
                 ++it )
                {
                const FileEntry* e = context.getSourceManager().getFileEntryForID( it->first );
                char* filename = new char[ strlen( e->getName()) + 100 ];
                sprintf( filename, "%s.new.%d", e->getName(), getpid());
                string error;
                bool ok = false;
                raw_fd_ostream ostream( filename, error );
                DiagnosticsEngine& diag = context.getDiagnostics();
                if( error.empty())
                    {
                    it->second.write( ostream );
                    ostream.close();
                    if( !ostream.has_error() && rename( filename, e->getName()) == 0 )
                        ok = true;
                    }
                ostream.clear_error();
                unlink( filename );
                if( !ok )
                    diag.Report( diag.getCustomDiagID( DiagnosticsEngine::Error,
                        "cannot write modified source to %0 (%1) [loplugin]" )) << e->getName() << error;
                delete[] filename;
                }
            }
    private:
        bool isArg( const char* arg ) const
            {
            return find( args.begin(), args.end(), arg ) != args.end();
            }
        Rewriter rewriter;
        vector< string > args;
        BodyNotInBlock bodyNotInBlock;
        LclStaticFix lclStaticFix;
        PostfixIncrementFix postfixIncrementFix;
        SalLogAreas salLogAreas;
        UnusedVariableCheck unusedVariableCheck;
    };

/**
 The Clang plugin class, just forwards to PluginHandler.
*/
class LibreOfficeAction
    : public PluginASTAction
    {
    public:
        virtual ASTConsumer* CreateASTConsumer( CompilerInstance& Compiler, StringRef InFile )
            {
            return new PluginHandler( Compiler.getASTContext(), _args );
            }
        virtual bool ParseArgs( const CompilerInstance& CI, const vector< string >& args )
            {
            _args = args;
            return true;
            }
    private:
        vector< string > _args;
    };

} // namespace

static FrontendPluginRegistry::Add< loplugin::LibreOfficeAction > X( "loplugin", "LibreOffice compile check plugin" );