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