clang-tools  8.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()->getBeginLoc(), "default parameter was declared here",
31  DiagnosticIDs::Note);
32  } else if (const ParmVarDecl *D =
33  Result.Nodes.getNodeAs<ParmVarDecl>("decl")) {
34  SourceRange DefaultArgRange = D->getDefaultArgRange();
35 
36  if (DefaultArgRange.getEnd() != D->getEndLoc()) {
37  return;
38  } else if (DefaultArgRange.getBegin().isMacroID()) {
39  diag(D->getBeginLoc(),
40  "declaring a parameter with a default argument is disallowed");
41  } else {
42  SourceLocation StartLocation =
43  D->getName().empty() ? D->getBeginLoc() : D->getLocation();
44 
45  SourceRange RemovalRange(Lexer::getLocForEndOfToken(
46  StartLocation, 0,
47  *Result.SourceManager,
48  Result.Context->getLangOpts()
49  ),
50  DefaultArgRange.getEnd()
51  );
52 
53  diag(D->getBeginLoc(),
54  "declaring a parameter with a default argument is disallowed")
55  << D << FixItHint::CreateRemoval(RemovalRange);
56  }
57  }
58 }
59 
60 } // namespace fuchsia
61 } // namespace tidy
62 } // namespace clang
const Decl * D
Definition: XRefs.cpp:79
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//