summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/sallogareas.cxx
blob: 02df2793824e195d3b10b2890dcdc8d0873cd315 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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.
 *
 */
#ifndef LO_CLANG_SHARED_PLUGINS

#include "plugin.hxx"
#include "check.hxx"
#include "compat.hxx"

#include <clang/Lex/Lexer.h>

#include <fstream>
#include <set>

namespace
{

class SalLogAreas
    : public loplugin::FilteringPlugin< SalLogAreas >
    {
    public:
        explicit SalLogAreas( const loplugin::InstantiationData& data )
            : FilteringPlugin(data), inFunction(nullptr) {}

        bool preRun() override {
            return true;
        }

        void run() override {
            if (preRun())
                {
                lastSalDetailLogStreamMacro = SourceLocation();
                TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
                }
        }

        bool VisitFunctionDecl( const FunctionDecl* function );
        bool VisitCallExpr( const CallExpr* call );
    private:
        void checkArea( StringRef area, SourceLocation location );
        void checkAreaSyntax(StringRef area, SourceLocation location);
        void readLogAreas();
        const FunctionDecl* inFunction;
        SourceLocation lastSalDetailLogStreamMacro;
        std::set< std::string > logAreas;
#if 0
        std::string firstSeenLogArea;
        SourceLocation firstSeenLocation;
#endif
    };

/*
This is a compile check.

Check area used in SAL_INFO/SAL_WARN macros against the list in include/sal/log-areas.dox and
report if the area is not listed there. The fix is either use a proper area or add it to the list
if appropriate.
*/

bool SalLogAreas::VisitFunctionDecl( const FunctionDecl* function )
    {
    inFunction = function;
    return true;
    }

bool SalLogAreas::VisitCallExpr( const CallExpr* call )
    {
    if( ignoreLocation( call ))
        return true;
    const FunctionDecl* func = call->getDirectCallee();
    if( !func )
        return true;

    if( !( func->getNumParams() == 5 && func->getIdentifier() != NULL
          && ( func->getName() == "sal_detail_log" || func->getName() == "log" || func->getName() == "DbgUnhandledException")) )
        return true;

    auto tc = loplugin::DeclCheck(func);
    enum class LogCallKind { Sal, DbgUnhandledException};
    LogCallKind kind;
    int areaArgIndex;
    if( tc.Function("sal_detail_log") || tc.Function("log").Namespace("detail").Namespace("sal").GlobalNamespace() )
        {
        kind = LogCallKind::Sal; // fine
        areaArgIndex = 1;
        }
    else if( tc.Function("DbgUnhandledException").GlobalNamespace() )
        {
        kind = LogCallKind::DbgUnhandledException; // ok
        areaArgIndex = 3;
        }
    else
        return true;

    // The SAL_DETAIL_LOG_STREAM macro expands to two calls to sal::detail::log(),
    // so do not warn repeatedly about the same macro (the area->getLocStart() of all the calls
    // from the same macro should be the same).
    if( kind == LogCallKind::Sal )
        {
        SourceLocation expansionLocation = compiler.getSourceManager().getExpansionLoc( compat::getBeginLoc(call));
        if( expansionLocation == lastSalDetailLogStreamMacro )
            return true;
        lastSalDetailLogStreamMacro = expansionLocation;
        };
    if( const clang::StringLiteral* area = dyn_cast< clang::StringLiteral >( call->getArg( areaArgIndex )->IgnoreParenImpCasts()))
        {
        if( area->getKind() == clang::StringLiteral::Ascii )
            checkArea( area->getBytes(), area->getExprLoc());
        else
            report( DiagnosticsEngine::Warning, "unsupported string literal kind (plugin needs fixing?)",
                compat::getBeginLoc(area));
        return true;
        }
    if( loplugin::DeclCheck(inFunction).Function("log").Namespace("detail").Namespace("sal").GlobalNamespace()
        || loplugin::DeclCheck(inFunction).Function("sal_detail_logFormat").GlobalNamespace() )
        return true; // These functions only forward to sal_detail_log, so ok.
    if( call->getArg( areaArgIndex )->isNullPointerConstant( compiler.getASTContext(),
        Expr::NPC_ValueDependentIsNotNull ) != Expr::NPCK_NotNull )
        { // If the area argument is a null pointer, that is allowed only for SAL_DEBUG.
        const SourceManager& source = compiler.getSourceManager();
        for( SourceLocation loc = compat::getBeginLoc(call);
             loc.isMacroID();
             loc = compat::getImmediateExpansionRange(source, loc ).first )
            {
            StringRef inMacro = Lexer::getImmediateMacroName( loc, source, compiler.getLangOpts());
            if( inMacro == "SAL_DEBUG" || inMacro == "SAL_DEBUG_BACKTRACE" )
                return true; // ok
            }
        report( DiagnosticsEngine::Warning, "missing log area",
            compat::getBeginLoc(call->getArg( 1 )->IgnoreParenImpCasts()));
        return true;
        }
    report( DiagnosticsEngine::Warning, "cannot analyse log area argument (plugin needs fixing?)",
        compat::getBeginLoc(call));
    return true;
    }

void SalLogAreas::checkArea( StringRef area, SourceLocation location )
    {
    if( logAreas.empty())
        readLogAreas();
    if( !logAreas.count( area ))
        {
        report( DiagnosticsEngine::Warning, "unknown log area '%0' (check or extend include/sal/log-areas.dox)",
            location ) << area;
        checkAreaSyntax(area, location);
        return;
        }
// don't leave this alive by default, generates too many false+
#if 0
    if (compiler.getSourceManager().isInMainFile(location))
        {
        auto matchpair = [this,area](StringRef p1, StringRef p2) {
            return (area == p1 && firstSeenLogArea == p2) || (area == p2 && firstSeenLogArea == p1);
        };
        // these are "cross-module" log areas
        if (area == "i18n" || area == "lok" || area == "lok.tiledrendering")
            ;
        // these appear to be cross-file log areas
        else if (  area == "chart2"
                || area == "oox.cscode" || area == "oox.csdata"
                || area == "slideshow.verbose"
                || area == "sc.opencl"
                || area == "sc.core.formulagroup"
                || area == "sw.pageframe" || area == "sw.idle" || area == "sw.level2"
                || area == "sw.docappend" || area == "sw.mailmerge"
                || area == "sw.uno"
                || area == "vcl.layout" || area == "vcl.a11y"
                || area == "vcl.gdi.fontmetric" || area == "vcl.opengl"
                || area == "vcl.harfbuzz" || area == "vcl.eventtesting"
                || area == "vcl.schedule" || area == "vcl.unity"
                || area == "xmlsecurity.comp"
                )
            ;
        else if (firstSeenLogArea == "")
            {
                firstSeenLogArea = area;
                firstSeenLocation = location;
            }
        // some modules do this deliberately
        else if (firstSeenLogArea.compare(0, 3, "jfw") == 0
                || firstSeenLogArea.compare(0, 6, "opencl") == 0)
            ;
        // mixing these in the same file seems legitimate
        else if (
                   matchpair("chart2.pie.label.bestfit", "chart2.pie.label.bestfit.inside")
                || matchpair("editeng", "editeng.chaining")
                || matchpair("oox.drawingml", "oox.cscode")
                || matchpair("oox.drawingml", "oox.drawingml.gradient")
                || matchpair("sc.core", "sc.core.grouparealistener")
                || matchpair("sc.orcus", "sc.orcus.condformat")
                || matchpair("sc.orcus", "sc.orcus.style")
                || matchpair("sc.orcus", "sc.orcus.autofilter")
                || matchpair("svx", "svx.chaining")
                || matchpair("sw.ww8", "sw.ww8.level2")
                || matchpair("writerfilter", "writerfilter.profile")
                )
            ;
        else if (firstSeenLogArea != area)
            {
            report( DiagnosticsEngine::Warning, "two different log areas '%0' and '%1' in the same file?",
                location ) << firstSeenLogArea << area;
            report( DiagnosticsEngine::Note, "first area was seen here",
                firstSeenLocation );
            }
        }
#endif
    }

void SalLogAreas::checkAreaSyntax(StringRef area, SourceLocation location) {
    for (std::size_t i = 0;;) {
        std::size_t j = area.find('.', i);
        if (j == StringRef::npos) {
            j = area.size();
        }
        if (j == i) {
            goto bad;
        }
        for (; i != j; ++i) {
            auto c = area[i];
            if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))) {
                goto bad;
            }
        }
        if (j == area.size()) {
            return;
        }
        i = j + 1;
    }
bad:
    report(
        DiagnosticsEngine::Warning,
        "invalid log area syntax '%0'%1 (see include/sal/log.hxx for details)",
        location)
        << area << (location.isValid() ? "" : " in include/sal/log-areas.dox");
}

void SalLogAreas::readLogAreas()
    {
    std::ifstream is( SRCDIR "/include/sal/log-areas.dox" );
    while( is.good())
        {
        std::string line;
        getline( is, line );
        size_t pos = line.find( "@li @c " );
        if( pos != std::string::npos )
            {
            pos += strlen( "@li @c " );
            size_t end = line.find( ' ', pos );
            std::string area;
            if( end == std::string::npos )
                area = line.substr( pos );
            else if( pos != end )
                area = line.substr( pos, end - pos );
            checkAreaSyntax(area, SourceLocation());
            logAreas.insert(area);
            }
        }
    // If you get this error message, you possibly have too old icecream (ICECC_EXTRAFILES is needed).
    if( logAreas.empty())
        report( DiagnosticsEngine::Warning, "error reading log areas" );
    }

static loplugin::Plugin::Registration< SalLogAreas > sallogareas( "sallogareas" );

} // namespace

#endif // LO_CLANG_SHARED_PLUGINS

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */