comparison clang-tools-extra/clang-tidy/fuchsia/DefaultArgumentsDeclarationsCheck.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
147:c2174574ed3a 150:1d019706d866
1 //===--- DefaultArgumentsDeclarationsCheck.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 "DefaultArgumentsDeclarationsCheck.h"
10
11 using namespace clang::ast_matchers;
12
13 namespace clang {
14 namespace tidy {
15 namespace fuchsia {
16
17 void DefaultArgumentsDeclarationsCheck::registerMatchers(MatchFinder *Finder) {
18 // Declaring default parameters is disallowed.
19 Finder->addMatcher(parmVarDecl(hasDefaultArgument()).bind("decl"), this);
20 }
21
22 void DefaultArgumentsDeclarationsCheck::check(
23 const MatchFinder::MatchResult &Result) {
24 const auto *D = Result.Nodes.getNodeAs<ParmVarDecl>("decl");
25 if (!D)
26 return;
27
28 SourceRange DefaultArgRange = D->getDefaultArgRange();
29
30 if (DefaultArgRange.getEnd() != D->getEndLoc())
31 return;
32
33 if (DefaultArgRange.getBegin().isMacroID()) {
34 diag(D->getBeginLoc(),
35 "declaring a parameter with a default argument is disallowed");
36 return;
37 }
38
39 SourceLocation StartLocation =
40 D->getName().empty() ? D->getBeginLoc() : D->getLocation();
41
42 SourceRange RemovalRange(
43 Lexer::getLocForEndOfToken(StartLocation, 0, *Result.SourceManager,
44 Result.Context->getLangOpts()),
45 DefaultArgRange.getEnd());
46
47 diag(D->getBeginLoc(),
48 "declaring a parameter with a default argument is disallowed")
49 << D << FixItHint::CreateRemoval(RemovalRange);
50 }
51
52 } // namespace fuchsia
53 } // namespace tidy
54 } // namespace clang