summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/indentation.cxx
blob: cf2512d1d25dde77ddfea55566a4339a7315fe47 (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
/* -*- 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 <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <unordered_set>
#include "config_clang.h"
#include "plugin.hxx"

/*
Check for child statements inside a compound statement that do not share the same indentation.

TODO if an open-brace starts on a new line by itself, check that it lines up with it's closing-brace
TODO else should line up with if
*/

namespace
{
class Indentation : public loplugin::FilteringPlugin<Indentation>
{
public:
    explicit Indentation(loplugin::InstantiationData const& data)
        : FilteringPlugin(data)
    {
    }

    virtual bool preRun() override
    {
        std::string fn(handler.getMainFileName());
        loplugin::normalizeDotDotInFilePath(fn);
        // include another file to build a table
        if (fn == SRCDIR "/sc/source/core/tool/cellkeytranslator.cxx")
            return false;
        // weird structure
        if (fn == SRCDIR "/sc/source/core/tool/compiler.cxx")
            return false;
        // looks like lex/yacc output
        if (fn == SRCDIR "/hwpfilter/source/grammar.cxx")
            return false;
        // the QEMIT macros
        if (loplugin::hasPathnamePrefix(fn, SRCDIR "/vcl/qt5/")
            || loplugin::hasPathnamePrefix(fn, SRCDIR "/vcl/qt6/")
            || loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/gtk3_kde5/kde5_filepicker_ipc.cxx"))
            return false;
        return true;
    }

    virtual void run() override
    {
        if (preRun())
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool VisitCompoundStmt(CompoundStmt const*);
    bool PreTraverseSwitchStmt(SwitchStmt*);
    bool PostTraverseSwitchStmt(SwitchStmt*, bool);
    bool TraverseSwitchStmt(SwitchStmt*);
    bool VisitSwitchStmt(SwitchStmt const*);
    bool VisitIfStmt(IfStmt const*);
    bool VisitForStmt(ForStmt const*);
    bool VisitWhileStmt(WhileStmt const*);
    bool VisitDoStmt(DoStmt const*);
    bool VisitCXXForRangeStmt(CXXForRangeStmt const*);

private:
    void checkCompoundStmtBraces(const Stmt* parentStmt, const CompoundStmt*);

    std::vector<const Stmt*> switchStmtBodies;
    std::unordered_set<const Stmt*> chainedSet;
};

bool Indentation::PreTraverseSwitchStmt(SwitchStmt* switchStmt)
{
    switchStmtBodies.push_back(switchStmt->getBody());
    return true;
}

bool Indentation::PostTraverseSwitchStmt(SwitchStmt*, bool)
{
    switchStmtBodies.pop_back();
    return true;
}

bool Indentation::TraverseSwitchStmt(SwitchStmt* switchStmt)
{
    PreTraverseSwitchStmt(switchStmt);
    auto ret = FilteringPlugin::TraverseSwitchStmt(switchStmt);
    PostTraverseSwitchStmt(switchStmt, ret);
    return ret;
}

bool Indentation::VisitCompoundStmt(CompoundStmt const* compoundStmt)
{
    if (ignoreLocation(compoundStmt))
        return true;
    // these are kind of free form
    if (!switchStmtBodies.empty() && switchStmtBodies.back() == compoundStmt)
        return true;

    constexpr unsigned MAX = std::numeric_limits<unsigned>::max();
    unsigned column = MAX;
    Stmt const* firstStmt = nullptr;
    unsigned curLine = MAX;
    unsigned prevLine = MAX;
    SourceLocation prevEnd;
    auto& SM = compiler.getSourceManager();
    for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
    {
        auto stmt = *i;
        auto const actualPrevEnd = prevEnd;
        prevEnd = stmt->getEndLoc(); // compute early, before below `continue`s

        // these show up in macro expansions, not interesting
        if (isa<NullStmt>(stmt))
            continue;
        // these are always weirdly indented
        if (isa<LabelStmt>(stmt))
            continue;

        auto stmtLoc = stmt->getBeginLoc();

        StringRef macroName;
        bool partOfMacro = false;
        if (SM.isMacroArgExpansion(stmtLoc) || SM.isMacroBodyExpansion(stmtLoc))
        {
            partOfMacro = true;
            macroName = Lexer::getImmediateMacroNameForDiagnostics(
                stmtLoc, compiler.getSourceManager(), compiler.getLangOpts());
            // CPPUNIT_TEST_SUITE/CPPUNIT_TEST/CPPUNIT_TEST_SUITE_END work together, so the one is indented inside the other
            if (macroName == "CPPUNIT_TEST_SUITE")
                continue;
            // similar thing in dbaccess/
            if (macroName == "DECL_PROP_IMPL")
                continue;
            // similar thing in forms/
            if (macroName == "DECL_IFACE_PROP_IMPL" || macroName == "DECL_BOOL_PROP_IMPL")
                continue;
            stmtLoc = SM.getExpansionRange(stmtLoc).getBegin();
        }

        // check for comment to the left of the statement
        {
            const char* p1 = SM.getCharacterData(stmtLoc);
            --p1;
            bool foundComment = false;
            while (*p1 && *p1 != '\n')
            {
                if (*p1 == '/')
                {
                    foundComment = true;
                    break;
                }
                --p1;
            }
            if (foundComment)
                continue;
        }

        bool invalid1 = false;
        bool invalid2 = false;
        unsigned tmpColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
        unsigned tmpLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
        if (invalid1 || invalid2)
            continue;
        prevLine = curLine;
        curLine = tmpLine;
        if (column == MAX)
        {
            column = tmpColumn;
            firstStmt = stmt;
        }
        else if (curLine == prevLine)
        {
            // ignore multiple statements on same line
        }
        else if (column != tmpColumn)
        {
            if (containsPreprocessingConditionalInclusion(SourceRange(
                    locationAfterToken(compiler.getSourceManager().getExpansionLoc(actualPrevEnd)),
                    compiler.getSourceManager().getExpansionLoc(stmt->getBeginLoc()))))
                continue;
            report(DiagnosticsEngine::Warning, "statement mis-aligned compared to neighbours %0",
                   stmtLoc)
                << macroName;
            report(DiagnosticsEngine::Note, "measured against this one", firstStmt->getBeginLoc());
            //getParentStmt(compoundStmt)->dump();
            //stmt->dump();
        }

        if (!partOfMacro)
            if (auto ifStmt = dyn_cast<IfStmt>(stmt))
            {
                auto bodyStmt = ifStmt->getThen();
                if (bodyStmt && !isa<CompoundStmt>(bodyStmt))
                {
                    stmtLoc = bodyStmt->getBeginLoc();
                    invalid1 = false;
                    invalid2 = false;
                    unsigned bodyColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
                    unsigned bodyLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
                    if (invalid1 || invalid2)
                        return true;

                    if (bodyLine != tmpLine && bodyColumn <= tmpColumn)
                        report(DiagnosticsEngine::Warning, "if body should be indented", stmtLoc);
                }

                auto elseStmt = ifStmt->getElse();
                if (elseStmt && !isa<CompoundStmt>(elseStmt) && !isa<IfStmt>(elseStmt))
                {
                    stmtLoc = elseStmt->getBeginLoc();
                    invalid1 = false;
                    invalid2 = false;
                    unsigned elseColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
                    unsigned elseLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
                    if (invalid1 || invalid2)
                        return true;
                    if (elseLine != tmpLine && elseColumn <= tmpColumn)
                        report(DiagnosticsEngine::Warning, "else body should be indented", stmtLoc);
                }
                if (elseStmt && !isa<CompoundStmt>(bodyStmt))
                {
                    stmtLoc = ifStmt->getElseLoc();
                    invalid1 = false;
                    invalid2 = false;
                    unsigned elseColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
                    unsigned elseLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
                    if (invalid1 || invalid2)
                        return true;
                    if (elseLine != tmpLine && elseColumn != tmpColumn)
                        report(DiagnosticsEngine::Warning, "if and else not aligned", stmtLoc);
                }
            }
    }

    return true;
}

bool Indentation::VisitIfStmt(IfStmt const* ifStmt)
{
    if (ignoreLocation(ifStmt))
        return true;

    // TODO - ignore chained if statements for now
    if (auto chained = ifStmt->getElse())
        chainedSet.insert(chained);
    if (chainedSet.find(ifStmt) != chainedSet.end())
        return true;

    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(ifStmt->getThen()))
        checkCompoundStmtBraces(ifStmt, compoundStmt);
    // TODO - needs to be checked against the line that contains the else keyword, but not against the parent
    //    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(ifStmt->getElse()))
    //        checkCompoundStmtBraces(ifStmt, compoundStmt);
    return true;
}

bool Indentation::VisitForStmt(ForStmt const* forStmt)
{
    if (ignoreLocation(forStmt))
        return true;
    if (chainedSet.find(forStmt) != chainedSet.end())
        return true;
    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(forStmt->getBody()))
        checkCompoundStmtBraces(forStmt, compoundStmt);
    return true;
}

bool Indentation::VisitWhileStmt(WhileStmt const* whileStmt)
{
    if (ignoreLocation(whileStmt))
        return true;
    if (chainedSet.find(whileStmt) != chainedSet.end())
        return true;
    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(whileStmt->getBody()))
        checkCompoundStmtBraces(whileStmt, compoundStmt);
    return true;
}

bool Indentation::VisitDoStmt(DoStmt const* doStmt)
{
    if (ignoreLocation(doStmt))
        return true;
    if (chainedSet.find(doStmt) != chainedSet.end())
        return true;
    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(doStmt->getBody()))
        checkCompoundStmtBraces(doStmt, compoundStmt);
    return true;
}

bool Indentation::VisitCXXForRangeStmt(CXXForRangeStmt const* cxxForRangeStmt)
{
    if (ignoreLocation(cxxForRangeStmt))
        return true;
    if (chainedSet.find(cxxForRangeStmt) != chainedSet.end())
        return true;
    if (auto compoundStmt = dyn_cast_or_null<CompoundStmt>(cxxForRangeStmt->getBody()))
        checkCompoundStmtBraces(cxxForRangeStmt, compoundStmt);
    return true;
}

void Indentation::checkCompoundStmtBraces(const Stmt* parentStmt, const CompoundStmt* compoundStmt)
{
    auto& SM = compiler.getSourceManager();
    bool invalid1 = false;
    bool invalid2 = false;

    auto parentBeginLoc = parentStmt->getBeginLoc();
    unsigned parentColumn = SM.getPresumedColumnNumber(parentBeginLoc, &invalid1);
    if (invalid1)
        return;

    auto startBraceLoc = compoundStmt->getLBracLoc();
    auto endBraceLoc = compoundStmt->getRBracLoc();
    unsigned beginColumn = SM.getPresumedColumnNumber(startBraceLoc, &invalid1);
    unsigned beginLine = SM.getPresumedLineNumber(startBraceLoc, &invalid2);
    if (invalid1 || invalid2)
        return;
    unsigned endColumn = SM.getPresumedColumnNumber(endBraceLoc, &invalid1);
    unsigned endLine = SM.getPresumedLineNumber(endBraceLoc, &invalid2);
    if (invalid1 || invalid2)
        return;
    if (beginLine == endLine)
        return;

    // check for code to the left of the starting brace
    bool foundCodeToLeft = false;
    {
        const char* p1 = SM.getCharacterData(startBraceLoc);
        --p1;
        while (*p1 && *p1 != '\n')
        {
            if (*p1 != ' ')
            {
                foundCodeToLeft = true;
                break;
            }
            --p1;
        }
    }

    // if we found code to the left of the start brace, that means the end-brace needs
    // to line up with the start of the parent statement
    if (foundCodeToLeft)
    {
        if (parentColumn != endColumn)
        {
            report(DiagnosticsEngine::Warning, "end brace not aligned with beginning of statement",
                   endBraceLoc);
            report(DiagnosticsEngine::Note, "statement beginning here", parentBeginLoc);
        }
        return;
    }

    if (parentColumn != beginColumn)
    {
        report(DiagnosticsEngine::Warning,
               "start brace not aligned with beginning of parent statement", startBraceLoc);
        report(DiagnosticsEngine::Note, "statement beginning here", parentBeginLoc);
    }
    else if (beginColumn != endColumn)
    {
        report(DiagnosticsEngine::Warning, "start and end brace not aligned", endBraceLoc);
        report(DiagnosticsEngine::Note, "start brace here", startBraceLoc);
    }

    /** now check that lines inside the compoundstmt are indented */
    if (!compoundStmt->size())
        return;
    auto firstStmt = compoundStmt->body_front();
    if (isa<LabelStmt>(firstStmt))
        return;
    auto firstStmtLoc = firstStmt->getBeginLoc();
    unsigned firstStmtBeginColumn = SM.getPresumedColumnNumber(firstStmtLoc, &invalid1);
    if (invalid1)
        return;
    if (firstStmtBeginColumn > beginColumn)
        return;
    StringRef fn = getFilenameOfLocation(compiler.getSourceManager().getSpellingLoc(firstStmtLoc));
    // this is doing code generation, so the weird layout is deliberate
    if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/core/opencl/"))
        return;
    report(DiagnosticsEngine::Warning, "body inside brace not indented", firstStmtLoc);
}

bool Indentation::VisitSwitchStmt(SwitchStmt const* switchStmt)
{
    if (ignoreLocation(switchStmt))
        return true;

    constexpr unsigned MAX = std::numeric_limits<unsigned>::max();
    unsigned column = MAX;
    Stmt const* firstStmt = nullptr;
    unsigned curLine = MAX;
    unsigned prevLine = MAX;
    auto& SM = compiler.getSourceManager();
    auto compoundStmt = dyn_cast<CompoundStmt>(switchStmt->getBody());
    if (!compoundStmt)
        return true;
    for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
    {
        Stmt const* caseStmt = dyn_cast<CaseStmt>(*i);
        if (!caseStmt)
            caseStmt = dyn_cast<DefaultStmt>(*i);
        if (!caseStmt)
            continue;

        auto stmtLoc = caseStmt->getBeginLoc();

        bool invalid1 = false;
        bool invalid2 = false;
        unsigned tmpColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
        unsigned tmpLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
        if (invalid1 || invalid2)
            continue;
        prevLine = curLine;
        curLine = tmpLine;
        if (column == MAX)
        {
            column = tmpColumn;
            firstStmt = caseStmt;
        }
        else if (curLine == prevLine)
        {
            // ignore multiple statements on same line
        }
        else if (column != tmpColumn)
        {
            // disable this for now, ends up touching some very large switch statements in sw/ and sc/
            (void)firstStmt;
            //            report(DiagnosticsEngine::Warning, "statement mis-aligned compared to neighbours",
            //                   stmtLoc);
            //            report(DiagnosticsEngine::Note, "measured against this one",
            //                   firstStmt->getBeginLoc());
            //getParentStmt(compoundStmt)->dump();
            //stmt->dump();
        }
    }
    return true;
}

loplugin::Plugin::Registration<Indentation> indentation("indentation");

} // namespace

#endif // LO_CLANG_SHARED_PLUGINS

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