summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/bodynotinblock.cxx
blob: ca4f904992c87ade4cfa6d260bc1696a932c0873 (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
/* -*- 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.
 *
 */

#include "bodynotinblock.hxx"

namespace loplugin
{

/*
This is a compile check.

Check for two statements that are both indented to look like a body of if/while/for
but are not inside a compound statement and thus the second one is unrelated.

For example:

    if( a != 0 )
        b = 2;
        c = 3;

Here either both statements should be inside {} or the second statement in indented wrong.
*/

BodyNotInBlock::BodyNotInBlock( const InstantiationData& data )
    : Plugin( data )
    {
    }

void BodyNotInBlock::run()
    {
    TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
    }

bool BodyNotInBlock::VisitIfStmt( const IfStmt* stmt )
    {
    if( ignoreLocation( stmt ))
        return true;
    checkBody( stmt->getThen(), stmt->getIfLoc(), 0, stmt->getElse() != NULL );
    checkBody( stmt->getElse(), stmt->getElseLoc(), 0 );
    return true;
    }

bool BodyNotInBlock::VisitWhileStmt( const WhileStmt* stmt )
    {
    if( ignoreLocation( stmt ))
        return true;
    checkBody( stmt->getBody(), stmt->getWhileLoc(), 1 );
    return true;
    }

bool BodyNotInBlock::VisitForStmt( const ForStmt* stmt )
    {
    if( ignoreLocation( stmt ))
        return true;
    checkBody( stmt->getBody(), stmt->getForLoc(), 2 );
    return true;
    }

bool BodyNotInBlock::VisitCXXForRangeStmt( const CXXForRangeStmt* stmt )
    {
    if( ignoreLocation( stmt ))
        return true;
    checkBody( stmt->getBody(), stmt->getForLoc(), 2 );
    return true;
    }

void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, int stmtType, bool dontGoUp )
    {
    if( body == NULL )
        return;
    // TODO: If the if/else/while/for comes from a macro expansion, ignore it completely for
    // now. The code below could assume everything is in the same place (and thus also column)
    // and give a false warning. Moreover some macros are rather loosely written and would
    // result in poor formatting. To be evaluated later, maybe this could be handled
    // including macro expansion.
    if( stmtLocation.isMacroID())
        return;
    if( dyn_cast< CompoundStmt >( body ))
        return; // if body is a compound statement, then it is in {}
    const Stmt* previousParent = parentStmt( body ); // Here the statement itself.
    // Find the next statement (in source position) after 'body'.
    for(;;)
        {
        const Stmt* parent = parentStmt( previousParent );
        if( parent == NULL )
            break;
        for( ConstStmtIterator it = parent->child_begin();
             it != parent->child_end();
             )
            {
            if( *it == previousParent ) // found grand(grand...)parent
                {
                // get next statement after our (grand...)parent
                ++it;
                while( it != parent->child_end() && *it == NULL )
                    ++it; // skip empty ones (missing 'else' bodies for example)
                if( it != parent->child_end())
                    {
                    bool invalid1, invalid2;
                    unsigned bodyColumn = compiler.getSourceManager()
                        .getPresumedColumnNumber( body->getLocStart(), &invalid1 );
                    unsigned nextStatementColumn = compiler.getSourceManager()
                        .getPresumedColumnNumber( (*it)->getLocStart(), &invalid2 );
                    if( invalid1 || invalid2 )
                        return;
                    if( bodyColumn == nextStatementColumn )
                        {
                        report( DiagnosticsEngine::Warning,
                            "statement aligned as second statement in %select{if|while|for}0 body but not in a statement block",
                            (*it)->getLocStart()) << stmtType;
                        report( DiagnosticsEngine::Note,
                            "%select{if|while|for}0 body statement is here",
                            body->getLocStart()) << stmtType;
                        }
                    return;
                    }
                // else we need to go higher to find the next statement
                }
            else
                ++it;
            }
        // If going up would mean leaving a {} block, stop, because the } should
        // make it visible the two statements are not in the same body.
        if( dyn_cast< CompoundStmt >( parent ))
            return;
        // If the body to be checked is a body of an if statement that has also
        // an else part, don't go up, the else is after the body and should make
        // it clear the body does not continue there.
        if( dontGoUp )
            return;
        previousParent = parent;
        }
    }

static Plugin::Registration< BodyNotInBlock > X( "bodynotinblock" );

} // namespace

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