001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.coding;
021
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
029
030/**
031 * Checks for fall through in switch statements
032 * Finds locations where a case <b>contains</b> Java code -
033 * but lacks a break, return, throw or continue statement.
034 *
035 * <p>
036 * The check honors special comments to suppress warnings about
037 * the fall through. By default the comments "fallthru",
038 * "fall through", "falls through" and "fallthrough" are recognized.
039 * </p>
040 * <p>
041 * The following fragment of code will NOT trigger the check,
042 * because of the comment "fallthru" and absence of any Java code
043 * in case 5.
044 * </p>
045 * <pre>
046 * case 3:
047 *     x = 2;
048 *     // fallthru
049 * case 4:
050 * case 5:
051 * case 6:
052 *     break;
053 * </pre>
054 * <p>
055 * The recognized relief comment can be configured with the property
056 * {@code reliefPattern}. Default value of this regular expression
057 * is "fallthru|fall through|fallthrough|falls through".
058 * </p>
059 * <p>
060 * An example of how to configure the check is:
061 * </p>
062 * <pre>
063 * &lt;module name="FallThrough"&gt;
064 *     &lt;property name=&quot;reliefPattern&quot;
065 *                  value=&quot;Fall Through&quot;/&gt;
066 * &lt;/module&gt;
067 * </pre>
068 *
069 * @author o_sukhodolsky
070 */
071public class FallThroughCheck extends AbstractCheck {
072
073    /**
074     * A key is pointing to the warning message text in "messages.properties"
075     * file.
076     */
077    public static final String MSG_FALL_THROUGH = "fall.through";
078
079    /**
080     * A key is pointing to the warning message text in "messages.properties"
081     * file.
082     */
083    public static final String MSG_FALL_THROUGH_LAST = "fall.through.last";
084
085    /** Do we need to check last case group. */
086    private boolean checkLastCaseGroup;
087
088    /** Relief regexp to allow fall through to the next case branch. */
089    private Pattern reliefPattern = Pattern.compile("fallthru|falls? ?through");
090
091    @Override
092    public int[] getDefaultTokens() {
093        return new int[] {TokenTypes.CASE_GROUP};
094    }
095
096    @Override
097    public int[] getRequiredTokens() {
098        return getDefaultTokens();
099    }
100
101    @Override
102    public int[] getAcceptableTokens() {
103        return new int[] {TokenTypes.CASE_GROUP};
104    }
105
106    /**
107     * Set the relief pattern.
108     *
109     * @param pattern
110     *            The regular expression pattern.
111     */
112    public void setReliefPattern(Pattern pattern) {
113        reliefPattern = pattern;
114    }
115
116    /**
117     * Configures whether we need to check last case group or not.
118     * @param value new value of the property.
119     */
120    public void setCheckLastCaseGroup(boolean value) {
121        checkLastCaseGroup = value;
122    }
123
124    @Override
125    public void visitToken(DetailAST ast) {
126        final DetailAST nextGroup = ast.getNextSibling();
127        final boolean isLastGroup = nextGroup.getType() != TokenTypes.CASE_GROUP;
128        if (!isLastGroup || checkLastCaseGroup) {
129            final DetailAST slist = ast.findFirstToken(TokenTypes.SLIST);
130
131            if (slist != null && !isTerminated(slist, true, true)
132                && !hasFallThroughComment(ast, nextGroup)) {
133                if (isLastGroup) {
134                    log(ast, MSG_FALL_THROUGH_LAST);
135                }
136                else {
137                    log(nextGroup, MSG_FALL_THROUGH);
138                }
139            }
140        }
141    }
142
143    /**
144     * Checks if a given subtree terminated by return, throw or,
145     * if allowed break, continue.
146     * @param ast root of given subtree
147     * @param useBreak should we consider break as terminator.
148     * @param useContinue should we consider continue as terminator.
149     * @return true if the subtree is terminated.
150     */
151    private boolean isTerminated(final DetailAST ast, boolean useBreak,
152                                 boolean useContinue) {
153        final boolean terminated;
154
155        switch (ast.getType()) {
156            case TokenTypes.LITERAL_RETURN:
157            case TokenTypes.LITERAL_THROW:
158                terminated = true;
159                break;
160            case TokenTypes.LITERAL_BREAK:
161                terminated = useBreak;
162                break;
163            case TokenTypes.LITERAL_CONTINUE:
164                terminated = useContinue;
165                break;
166            case TokenTypes.SLIST:
167                terminated = checkSlist(ast, useBreak, useContinue);
168                break;
169            case TokenTypes.LITERAL_IF:
170                terminated = checkIf(ast, useBreak, useContinue);
171                break;
172            case TokenTypes.LITERAL_FOR:
173            case TokenTypes.LITERAL_WHILE:
174            case TokenTypes.LITERAL_DO:
175                terminated = checkLoop(ast);
176                break;
177            case TokenTypes.LITERAL_TRY:
178                terminated = checkTry(ast, useBreak, useContinue);
179                break;
180            case TokenTypes.LITERAL_SWITCH:
181                terminated = checkSwitch(ast, useContinue);
182                break;
183            default:
184                terminated = false;
185        }
186        return terminated;
187    }
188
189    /**
190     * Checks if a given SLIST terminated by return, throw or,
191     * if allowed break, continue.
192     * @param slistAst SLIST to check
193     * @param useBreak should we consider break as terminator.
194     * @param useContinue should we consider continue as terminator.
195     * @return true if SLIST is terminated.
196     */
197    private boolean checkSlist(final DetailAST slistAst, boolean useBreak,
198                               boolean useContinue) {
199        DetailAST lastStmt = slistAst.getLastChild();
200
201        if (lastStmt.getType() == TokenTypes.RCURLY) {
202            lastStmt = lastStmt.getPreviousSibling();
203        }
204
205        return lastStmt != null
206            && isTerminated(lastStmt, useBreak, useContinue);
207    }
208
209    /**
210     * Checks if a given IF terminated by return, throw or,
211     * if allowed break, continue.
212     * @param ast IF to check
213     * @param useBreak should we consider break as terminator.
214     * @param useContinue should we consider continue as terminator.
215     * @return true if IF is terminated.
216     */
217    private boolean checkIf(final DetailAST ast, boolean useBreak,
218                            boolean useContinue) {
219        final DetailAST thenStmt = ast.findFirstToken(TokenTypes.RPAREN)
220                .getNextSibling();
221        final DetailAST elseStmt = thenStmt.getNextSibling();
222        boolean isTerminated = isTerminated(thenStmt, useBreak, useContinue);
223
224        if (isTerminated && elseStmt != null) {
225            isTerminated = isTerminated(elseStmt.getFirstChild(),
226                useBreak, useContinue);
227        }
228        else if (elseStmt == null) {
229            isTerminated = false;
230        }
231        return isTerminated;
232    }
233
234    /**
235     * Checks if a given loop terminated by return, throw or,
236     * if allowed break, continue.
237     * @param ast loop to check
238     * @return true if loop is terminated.
239     */
240    private boolean checkLoop(final DetailAST ast) {
241        final DetailAST loopBody;
242        if (ast.getType() == TokenTypes.LITERAL_DO) {
243            final DetailAST lparen = ast.findFirstToken(TokenTypes.DO_WHILE);
244            loopBody = lparen.getPreviousSibling();
245        }
246        else {
247            final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN);
248            loopBody = rparen.getNextSibling();
249        }
250        return isTerminated(loopBody, false, false);
251    }
252
253    /**
254     * Checks if a given try/catch/finally block terminated by return, throw or,
255     * if allowed break, continue.
256     * @param ast loop to check
257     * @param useBreak should we consider break as terminator.
258     * @param useContinue should we consider continue as terminator.
259     * @return true if try/catch/finally block is terminated.
260     */
261    private boolean checkTry(final DetailAST ast, boolean useBreak,
262                             boolean useContinue) {
263        final DetailAST finalStmt = ast.getLastChild();
264        boolean isTerminated = false;
265        if (finalStmt.getType() == TokenTypes.LITERAL_FINALLY) {
266            isTerminated = isTerminated(finalStmt.findFirstToken(TokenTypes.SLIST),
267                                useBreak, useContinue);
268        }
269
270        if (!isTerminated) {
271            DetailAST firstChild = ast.getFirstChild();
272
273            if (firstChild.getType() == TokenTypes.RESOURCE_SPECIFICATION) {
274                firstChild = firstChild.getNextSibling();
275            }
276
277            isTerminated = isTerminated(firstChild,
278                    useBreak, useContinue);
279
280            DetailAST catchStmt = ast.findFirstToken(TokenTypes.LITERAL_CATCH);
281            while (catchStmt != null
282                    && isTerminated
283                    && catchStmt.getType() == TokenTypes.LITERAL_CATCH) {
284                final DetailAST catchBody =
285                        catchStmt.findFirstToken(TokenTypes.SLIST);
286                isTerminated = isTerminated(catchBody, useBreak, useContinue);
287                catchStmt = catchStmt.getNextSibling();
288            }
289        }
290        return isTerminated;
291    }
292
293    /**
294     * Checks if a given switch terminated by return, throw or,
295     * if allowed break, continue.
296     * @param literalSwitchAst loop to check
297     * @param useContinue should we consider continue as terminator.
298     * @return true if switch is terminated.
299     */
300    private boolean checkSwitch(final DetailAST literalSwitchAst, boolean useContinue) {
301        DetailAST caseGroup = literalSwitchAst.findFirstToken(TokenTypes.CASE_GROUP);
302        boolean isTerminated = caseGroup != null;
303        while (isTerminated && caseGroup.getType() != TokenTypes.RCURLY) {
304            final DetailAST caseBody =
305                caseGroup.findFirstToken(TokenTypes.SLIST);
306            isTerminated = caseBody != null && isTerminated(caseBody, false, useContinue);
307            caseGroup = caseGroup.getNextSibling();
308        }
309        return isTerminated;
310    }
311
312    /**
313     * Determines if the fall through case between {@code currentCase} and
314     * {@code nextCase} is relieved by a appropriate comment.
315     *
316     * @param currentCase AST of the case that falls through to the next case.
317     * @param nextCase AST of the next case.
318     * @return True if a relief comment was found
319     */
320    private boolean hasFallThroughComment(DetailAST currentCase, DetailAST nextCase) {
321        boolean allThroughComment = false;
322        final int endLineNo = nextCase.getLineNo();
323        final int endColNo = nextCase.getColumnNo();
324
325        // Remember: The lines number returned from the AST is 1-based, but
326        // the lines number in this array are 0-based. So you will often
327        // see a "lineNo-1" etc.
328        final String[] lines = getLines();
329
330        // Handle:
331        //    case 1:
332        //    /+ FALLTHRU +/ case 2:
333        //    ....
334        // and
335        //    switch(i) {
336        //    default:
337        //    /+ FALLTHRU +/}
338        //
339        final String linePart = lines[endLineNo - 1].substring(0, endColNo);
340        if (matchesComment(reliefPattern, linePart, endLineNo)) {
341            allThroughComment = true;
342        }
343        else {
344            // Handle:
345            //    case 1:
346            //    .....
347            //    // FALLTHRU
348            //    case 2:
349            //    ....
350            // and
351            //    switch(i) {
352            //    default:
353            //    // FALLTHRU
354            //    }
355            final int startLineNo = currentCase.getLineNo();
356            for (int i = endLineNo - 2; i > startLineNo - 1; i--) {
357                if (!CommonUtils.isBlank(lines[i])) {
358                    allThroughComment = matchesComment(reliefPattern, lines[i], i + 1);
359                    break;
360                }
361            }
362        }
363        return allThroughComment;
364    }
365
366    /**
367     * Does a regular expression match on the given line and checks that a
368     * possible match is within a comment.
369     * @param pattern The regular expression pattern to use.
370     * @param line The line of test to do the match on.
371     * @param lineNo The line number in the file.
372     * @return True if a match was found inside a comment.
373     */
374    private boolean matchesComment(Pattern pattern, String line, int lineNo
375    ) {
376        final Matcher matcher = pattern.matcher(line);
377
378        final boolean hit = matcher.find();
379
380        if (hit) {
381            final int startMatch = matcher.start();
382            // -1 because it returns the char position beyond the match
383            final int endMatch = matcher.end() - 1;
384            return getFileContents().hasIntersectionWithComment(lineNo,
385                    startMatch, lineNo, endMatch);
386        }
387        return false;
388    }
389}