1 | //===--- AvoidReturnWithVoidValueCheck.cpp - clang-tidy -------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "AvoidReturnWithVoidValueCheck.h" |
10 | #include "clang/AST/Stmt.h" |
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
12 | #include "clang/ASTMatchers/ASTMatchers.h" |
13 | |
14 | using namespace clang::ast_matchers; |
15 | |
16 | namespace clang::tidy::readability { |
17 | |
18 | static constexpr auto IgnoreMacrosName = "IgnoreMacros" ; |
19 | static constexpr auto IgnoreMacrosDefault = true; |
20 | |
21 | static constexpr auto StrictModeName = "StrictMode" ; |
22 | static constexpr auto StrictModeDefault = true; |
23 | |
24 | AvoidReturnWithVoidValueCheck::AvoidReturnWithVoidValueCheck( |
25 | StringRef Name, ClangTidyContext *Context) |
26 | : ClangTidyCheck(Name, Context), |
27 | IgnoreMacros( |
28 | Options.getLocalOrGlobal(LocalName: IgnoreMacrosName, Default: IgnoreMacrosDefault)), |
29 | StrictMode(Options.getLocalOrGlobal(LocalName: StrictModeName, Default: StrictModeDefault)) {} |
30 | |
31 | void AvoidReturnWithVoidValueCheck::registerMatchers(MatchFinder *Finder) { |
32 | Finder->addMatcher( |
33 | NodeMatch: returnStmt( |
34 | hasReturnValue(InnerMatcher: allOf(hasType(InnerMatcher: voidType()), unless(initListExpr()))), |
35 | optionally(hasParent(compoundStmt().bind(ID: "compound_parent" )))) |
36 | .bind(ID: "void_return" ), |
37 | Action: this); |
38 | } |
39 | |
40 | void AvoidReturnWithVoidValueCheck::check( |
41 | const MatchFinder::MatchResult &Result) { |
42 | const auto *VoidReturn = Result.Nodes.getNodeAs<ReturnStmt>(ID: "void_return" ); |
43 | if (IgnoreMacros && VoidReturn->getBeginLoc().isMacroID()) |
44 | return; |
45 | if (!StrictMode && !Result.Nodes.getNodeAs<CompoundStmt>(ID: "compound_parent" )) |
46 | return; |
47 | diag(Loc: VoidReturn->getBeginLoc(), Description: "return statement within a void function " |
48 | "should not have a specified return value" ); |
49 | } |
50 | |
51 | void AvoidReturnWithVoidValueCheck::storeOptions( |
52 | ClangTidyOptions::OptionMap &Opts) { |
53 | Options.store(Options&: Opts, LocalName: IgnoreMacrosName, Value: IgnoreMacros); |
54 | Options.store(Options&: Opts, LocalName: StrictModeName, Value: StrictMode); |
55 | } |
56 | |
57 | } // namespace clang::tidy::readability |
58 | |