Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "ExplicitImplicitChecker.h"
6
#include "CustomMatchers.h"
7
8
void ExplicitImplicitChecker::registerMatchers(MatchFinder *AstMatcher) {
9
AstMatcher->addMatcher(
10
cxxConstructorDecl(
11
isInterestingImplicitCtor(),
12
ofClass(allOf(isConcreteClass(), decl().bind("class"))),
13
unless(isMarkedImplicit()))
14
.bind("ctor"),
15
this);
16
}
17
18
void ExplicitImplicitChecker::check(const MatchFinder::MatchResult &Result) {
19
// We've already checked everything in the matcher, so we just have to report
20
// the error.
21
22
const CXXConstructorDecl *Ctor =
23
Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
24
const CXXRecordDecl *Declaration =
25
Result.Nodes.getNodeAs<CXXRecordDecl>("class");
26
27
FixItHint FixItHint =
28
FixItHint::CreateInsertion(Ctor->getLocation(), "explicit ");
29
diag(Ctor->getLocation(), "bad implicit conversion constructor for %0",
30
DiagnosticIDs::Error)
31
<< Declaration->getDeclName();
32
diag(Ctor->getLocation(),
33
"consider adding the explicit keyword to the constructor",
34
DiagnosticIDs::Note)
35
<< FixItHint;
36
}