Changeset View
Changeset View
Standalone View
Standalone View
unreachablevisitor.h
- This file was added.
1 | /***************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright 2017 Francis Herne <mail@flherne.uk> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 2 of * | ||||
7 | * the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||||
16 | ***************************************************************************** | ||||
17 | */ | ||||
18 | | ||||
19 | #ifndef PYTHON_UNREACHABLEVISITOR_H | ||||
20 | #define PYTHON_UNREACHABLEVISITOR_H | ||||
21 | | ||||
22 | #include <language/duchain/duchainlock.h> | ||||
23 | #include <language/duchain/problem.h> | ||||
24 | #include <language/duchain/topducontext.h> | ||||
25 | | ||||
26 | #include "astdefaultvisitor.h" | ||||
27 | #include "pythonduchainexport.h" | ||||
28 | | ||||
29 | #include <QDebug> | ||||
30 | | ||||
31 | using namespace KDevelop; | ||||
32 | | ||||
33 | namespace Python | ||||
34 | { | ||||
35 | | ||||
36 | /** | ||||
37 | * @brief Scan for (simple cases of) unreachable code and insert warnings. | ||||
38 | */ | ||||
39 | class KDEVPYTHONDUCHAIN_EXPORT UnreachableVisitor : public AstDefaultVisitor | ||||
40 | { | ||||
41 | public: | ||||
42 | UnreachableVisitor(const ReferencedTopDUContext& ctx) : | ||||
43 | AstDefaultVisitor(), m_topContext(ctx) {}; | ||||
44 | | ||||
45 | void addWarnings(Ast* node) { | ||||
46 | visitNode(node); | ||||
47 | m_currentlyUnreachable = false; | ||||
48 | } | ||||
49 | | ||||
50 | void visitNode(Ast* node) override | ||||
51 | { | ||||
52 | if ( node && m_currentlyUnreachable ) { | ||||
53 | DUChainWriteLocker lock; | ||||
54 | auto p = new Problem(); | ||||
55 | auto range = KTextEditor::Range(node->start(), 3); | ||||
56 | p->setDescription(i18n("Unreachable code")); | ||||
57 | p->setFinalLocation(DocumentRange(m_topContext->url(), range)); | ||||
58 | p->setSource(IProblem::SemanticAnalysis); | ||||
59 | p->setSeverity(IProblem::Warning); | ||||
60 | ProblemPointer ptr(p); | ||||
61 | m_topContext->addProblem(ptr); | ||||
62 | m_currentlyUnreachable = false; // Only one warning per instance | ||||
63 | } | ||||
64 | AstDefaultVisitor::visitNode(node); | ||||
65 | } | ||||
66 | | ||||
67 | void visitBreak(BreakAst* node) override | ||||
68 | { | ||||
69 | Q_UNUSED(node); | ||||
70 | m_currentlyUnreachable = true; | ||||
71 | } | ||||
72 | | ||||
73 | void visitContinue(ContinueAst* node) override | ||||
74 | { | ||||
75 | Q_UNUSED(node); | ||||
76 | m_currentlyUnreachable = true; | ||||
77 | } | ||||
78 | | ||||
79 | void visitReturn(ReturnAst* node) override | ||||
80 | { | ||||
81 | Q_UNUSED(node); // Child is never a statement, no point visiting | ||||
82 | m_currentlyUnreachable = true; | ||||
83 | } | ||||
84 | | ||||
85 | void visitRaise(Python::RaiseAst* node) override | ||||
86 | { | ||||
87 | Q_UNUSED(node); | ||||
88 | m_currentlyUnreachable = true; | ||||
89 | } | ||||
90 | | ||||
91 | void visitIf(Python::IfAst* node) override | ||||
92 | { | ||||
93 | // No need to visit the condition | ||||
94 | visitNodeList(node->body); | ||||
95 | bool unreachableAfterFirst = m_currentlyUnreachable; | ||||
96 | // `if` block doesn't affect `else`. | ||||
97 | m_currentlyUnreachable = false; | ||||
98 | visitNodeList(node->orelse); | ||||
99 | // Following code is unreachable only if both branches cause that. | ||||
100 | m_currentlyUnreachable &= unreachableAfterFirst; | ||||
101 | } | ||||
102 | | ||||
103 | void visitFor(ForAst* node) override | ||||
104 | { | ||||
105 | visitNodeList(node->body); | ||||
106 | // Loop body may not always be executed: `for x in []` | ||||
107 | m_currentlyUnreachable = false; | ||||
108 | visitNodeList(node->orelse); | ||||
109 | m_currentlyUnreachable = false; | ||||
110 | } | ||||
111 | | ||||
112 | void visitWhile(WhileAst* node) override | ||||
113 | { | ||||
114 | visitNodeList(node->body); | ||||
115 | m_currentlyUnreachable = false; | ||||
116 | visitNodeList(node->orelse); | ||||
117 | m_currentlyUnreachable = false; | ||||
118 | } | ||||
119 | | ||||
120 | void visitTry(TryAst* node) override | ||||
121 | { | ||||
122 | visitNodeList(node->body); | ||||
123 | m_currentlyUnreachable = false; | ||||
124 | visitNodeList(node->handlers); | ||||
125 | visitNodeList(node->orelse); | ||||
126 | m_currentlyUnreachable = false; | ||||
127 | visitNodeList(node->finally); | ||||
128 | m_currentlyUnreachable = false; | ||||
129 | } | ||||
130 | | ||||
131 | void visitExceptionHandler(ExceptionHandlerAst* node) override | ||||
132 | { | ||||
133 | visitNodeList(node->body); | ||||
134 | m_currentlyUnreachable = false; | ||||
135 | } | ||||
136 | | ||||
137 | void visitFunctionDefinition(FunctionDefinitionAst* node) override | ||||
138 | { | ||||
139 | visitNodeList(node->body); | ||||
140 | m_currentlyUnreachable = false; | ||||
141 | } | ||||
142 | | ||||
143 | void visitClassDefinition(ClassDefinitionAst* node) override | ||||
144 | { | ||||
145 | visitNodeList(node->body); | ||||
146 | m_currentlyUnreachable = false; | ||||
147 | } | ||||
148 | | ||||
149 | private: | ||||
150 | const ReferencedTopDUContext m_topContext; | ||||
151 | bool m_currentlyUnreachable = false; | ||||
152 | }; | ||||
153 | | ||||
154 | } | ||||
155 | | ||||
156 | #endif // PYTHON_UNREACHABLEVISITOR_H |