clang-tools 10.0.0

DefaultArgumentsDeclarationsCheck.cpp
Go to the documentation of this file.
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
clang::ast_matchers
Definition: AbseilMatcher.h:14
DefaultArgumentsDeclarationsCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27