clang-tools  6.0.0
fuchsia/DefaultArgumentsCheck.cpp
Go to the documentation of this file.
1 //===--- DefaultArgumentsCheck.cpp - clang-tidy----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "DefaultArgumentsCheck.h"
11 
12 using namespace clang::ast_matchers;
13 
14 namespace clang {
15 namespace tidy {
16 namespace fuchsia {
17 
18 void DefaultArgumentsCheck::registerMatchers(MatchFinder *Finder) {
19  // Calling a function which uses default arguments is disallowed.
20  Finder->addMatcher(cxxDefaultArgExpr().bind("stmt"), this);
21  // Declaring default parameters is disallowed.
22  Finder->addMatcher(parmVarDecl(hasDefaultArgument()).bind("decl"), this);
23 }
24 
25 void DefaultArgumentsCheck::check(const MatchFinder::MatchResult &Result) {
26  if (const auto *S =
27  Result.Nodes.getNodeAs<CXXDefaultArgExpr>("stmt")) {
28  diag(S->getUsedLocation(),
29  "calling a function that uses a default argument is disallowed");
30  diag(S->getParam()->getLocStart(),
31  "default parameter was declared here",
32  DiagnosticIDs::Note);
33  } else if (const ParmVarDecl *D =
34  Result.Nodes.getNodeAs<ParmVarDecl>("decl")) {
35  SourceRange DefaultArgRange = D->getDefaultArgRange();
36 
37  if (DefaultArgRange.getEnd() != D->getLocEnd()) {
38  return;
39  } else if (DefaultArgRange.getBegin().isMacroID()) {
40  diag(D->getLocStart(),
41  "declaring a parameter with a default argument is disallowed");
42  } else {
43  SourceLocation StartLocation = D->getName().empty() ?
44  D->getLocStart() : D->getLocation();
45 
46  SourceRange RemovalRange(Lexer::getLocForEndOfToken(
47  StartLocation, 0,
48  *Result.SourceManager,
49  Result.Context->getLangOpts()
50  ),
51  DefaultArgRange.getEnd()
52  );
53 
54  diag(D->getLocStart(),
55  "declaring a parameter with a default argument is disallowed")
56  << D
57  << FixItHint::CreateRemoval(RemovalRange);
58  }
59  }
60 }
61 
62 } // namespace fuchsia
63 } // namespace tidy
64 } // namespace clang