10 #include "../utils/LexerUtils.h" 11 #include "clang/ASTMatchers/ASTMatchers.h" 12 #include "llvm/ADT/Optional.h" 13 #include "llvm/ADT/SmallVector.h" 19 namespace readability {
25 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
26 return InnerMatcher.matches(Node.getUnqualifiedType(), Finder,
Builder);
31 llvm::Optional<Token> findQualToken(
const VarDecl *
Decl,
Qualifier Qual,
32 const MatchFinder::MatchResult &Result) {
38 Qual == Qualifier::Restrict &&
"Invalid Qualifier");
40 SourceLocation BeginLoc = Decl->getQualifierLoc().getBeginLoc();
41 if (BeginLoc.isInvalid())
42 BeginLoc = Decl->getBeginLoc();
43 SourceLocation EndLoc = Decl->getLocation();
45 CharSourceRange FileRange = Lexer::makeFileCharRange(
46 CharSourceRange::getCharRange(BeginLoc, EndLoc), *Result.SourceManager,
47 Result.Context->getLangOpts());
49 if (FileRange.isInvalid())
55 : Qual == Qualifier::Volatile ? tok::kw_volatile : tok::kw_restrict;
58 *Result.SourceManager);
61 llvm::Optional<SourceRange>
62 getTypeSpecifierLocation(
const VarDecl *Var,
63 const MatchFinder::MatchResult &Result) {
64 SourceRange TypeSpecifier(
65 Var->getTypeSpecStartLoc(),
66 Var->getTypeSpecEndLoc().getLocWithOffset(Lexer::MeasureTokenLength(
67 Var->getTypeSpecEndLoc(), *Result.SourceManager,
68 Result.Context->getLangOpts())));
70 if (TypeSpecifier.getBegin().isMacroID() ||
71 TypeSpecifier.getEnd().isMacroID())
76 llvm::Optional<SourceRange> mergeReplacementRange(SourceRange &TypeSpecifier,
77 const Token &ConstToken) {
78 if (TypeSpecifier.getBegin().getLocWithOffset(-1) == ConstToken.getEndLoc()) {
79 TypeSpecifier.setBegin(ConstToken.getLocation());
82 if (TypeSpecifier.getEnd().getLocWithOffset(1) == ConstToken.getLocation()) {
83 TypeSpecifier.setEnd(ConstToken.getEndLoc());
86 return SourceRange(ConstToken.getLocation(), ConstToken.getEndLoc());
89 bool isPointerConst(QualType QType) {
90 QualType Pointee = QType->getPointeeType();
91 assert(!Pointee.isNull() &&
"can't have a null Pointee");
92 return Pointee.isConstQualified();
95 bool isAutoPointerConst(QualType QType) {
97 cast<AutoType>(QType->getPointeeType().getTypePtr())->desugar();
98 assert(!Pointee.isNull() &&
"can't have a null Pointee");
99 return Pointee.isConstQualified();
104 void QualifiedAutoCheck::registerMatchers(MatchFinder *Finder) {
105 if (!getLangOpts().CPlusPlus11)
108 auto ExplicitSingleVarDecl =
109 [](
const ast_matchers::internal::Matcher<VarDecl> &InnerMatcher,
110 llvm::StringRef ID) {
112 unless(isInTemplateInstantiation()),
114 varDecl(unless(isImplicit()), InnerMatcher).bind(ID)));
116 auto ExplicitSingleVarDeclInTemplate =
117 [](
const ast_matchers::internal::Matcher<VarDecl> &InnerMatcher,
118 llvm::StringRef ID) {
120 isInTemplateInstantiation(),
122 varDecl(unless(isImplicit()), InnerMatcher).bind(ID)));
125 auto IsBoundToType = refersToType(equalsBoundNode(
"type"));
128 ExplicitSingleVarDecl(hasType(autoType(hasDeducedType(
129 pointerType(pointee(unless(functionType())))))),
134 ExplicitSingleVarDeclInTemplate(
135 allOf(hasType(autoType(hasDeducedType(pointerType(
136 pointee(hasUnqualifiedType(qualType().bind(
"type")),
137 unless(functionType())))))),
139 functionDecl(hasAnyTemplateArgument(IsBoundToType))),
140 hasAncestor(classTemplateSpecializationDecl(
141 hasAnyTemplateArgument(IsBoundToType))))),
144 Finder->addMatcher(ExplicitSingleVarDecl(
145 hasType(pointerType(pointee(autoType()))),
"auto_ptr"),
148 ExplicitSingleVarDecl(hasType(lValueReferenceType(pointee(autoType()))),
153 void QualifiedAutoCheck::check(
const MatchFinder::MatchResult &Result) {
154 if (
const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"auto")) {
155 SourceRange TypeSpecifier;
156 if (llvm::Optional<SourceRange> TypeSpec =
157 getTypeSpecifierLocation(Var, Result)) {
158 TypeSpecifier = *TypeSpec;
162 llvm::SmallVector<SourceRange, 4> RemoveQualifiersRange;
163 auto CheckQualifier = [&](
bool IsPresent, Qualifier Qual) {
165 llvm::Optional<Token> Token = findQualToken(Var, Qual, Result);
166 if (!Token || Token->getLocation().isMacroID())
168 if (llvm::Optional<SourceRange> Result =
169 mergeReplacementRange(TypeSpecifier, *Token))
170 RemoveQualifiersRange.push_back(*Result);
175 bool IsLocalConst = Var->getType().isLocalConstQualified();
176 bool IsLocalVolatile = Var->getType().isLocalVolatileQualified();
177 bool IsLocalRestrict = Var->getType().isLocalRestrictQualified();
181 if (CheckQualifier(IsLocalVolatile, Qualifier::Volatile))
183 if (CheckQualifier(IsLocalRestrict, Qualifier::Restrict))
187 if (Var->getLocation() == TypeSpecifier.getEnd().getLocWithOffset(1))
188 TypeSpecifier.setEnd(TypeSpecifier.getEnd().getLocWithOffset(1));
190 CharSourceRange FixItRange = CharSourceRange::getCharRange(TypeSpecifier);
191 if (FixItRange.isInvalid())
194 SourceLocation FixitLoc = FixItRange.getBegin();
195 for (SourceRange &
Range : RemoveQualifiersRange) {
196 if (
Range.getBegin() < FixitLoc)
197 FixitLoc =
Range.getBegin();
200 std::string ReplStr = [&] {
201 llvm::StringRef PtrConst = isPointerConst(Var->getType()) ?
"const " :
"";
202 llvm::StringRef LocalConst = IsLocalConst ?
"const " :
"";
203 llvm::StringRef LocalVol = IsLocalVolatile ?
"volatile " :
"";
204 llvm::StringRef LocalRestrict = IsLocalRestrict ?
"__restrict " :
"";
205 return (PtrConst +
"auto *" + LocalConst + LocalVol + LocalRestrict)
209 DiagnosticBuilder Diag =
210 diag(FixitLoc,
"'%0%1%2auto %3' can be declared as '%4%3'")
211 << (IsLocalConst ?
"const " :
"")
212 << (IsLocalVolatile ?
"volatile " :
"")
213 << (IsLocalRestrict ?
"__restrict " :
"") << Var->getName() << ReplStr;
215 for (SourceRange &
Range : RemoveQualifiersRange) {
216 Diag << FixItHint::CreateRemoval(
217 CharSourceRange::getCharRange(
Range.getBegin(),
Range.getEnd()));
220 Diag << FixItHint::CreateReplacement(FixItRange, ReplStr);
223 if (
const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"auto_ptr")) {
224 if (!isPointerConst(Var->getType()))
226 if (!isAutoPointerConst(Var->getType()))
230 if (Var->getType().isLocalConstQualified()) {
231 llvm::Optional<Token> Token =
233 if (!Token || Token->getLocation().isMacroID())
236 if (Var->getType().isLocalVolatileQualified()) {
237 llvm::Optional<Token> Token =
238 findQualToken(Var, Qualifier::Volatile, Result);
239 if (!Token || Token->getLocation().isMacroID())
242 if (Var->getType().isLocalRestrictQualified()) {
243 llvm::Optional<Token> Token =
244 findQualToken(Var, Qualifier::Restrict, Result);
245 if (!Token || Token->getLocation().isMacroID())
249 CharSourceRange FixItRange;
250 if (llvm::Optional<SourceRange> TypeSpec =
251 getTypeSpecifierLocation(Var, Result)) {
252 FixItRange = CharSourceRange::getCharRange(*TypeSpec);
253 if (FixItRange.isInvalid())
258 DiagnosticBuilder Diag =
259 diag(FixItRange.getBegin(),
260 "'auto *%0%1%2' can be declared as 'const auto *%0%1%2'")
261 << (Var->getType().isLocalConstQualified() ?
"const " :
"")
262 << (Var->getType().isLocalVolatileQualified() ?
"volatile " :
"")
264 Diag << FixItHint::CreateReplacement(FixItRange,
"const auto *");
267 if (
const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"auto_ref")) {
268 if (!isPointerConst(Var->getType()))
270 if (!isAutoPointerConst(Var->getType()))
274 CharSourceRange FixItRange;
275 if (llvm::Optional<SourceRange> TypeSpec =
276 getTypeSpecifierLocation(Var, Result)) {
277 FixItRange = CharSourceRange::getCharRange(*TypeSpec);
278 if (FixItRange.isInvalid())
283 DiagnosticBuilder Diag =
284 diag(FixItRange.getBegin(),
285 "'auto &%0' can be declared as 'const auto &%0'")
287 Diag << FixItHint::CreateReplacement(FixItRange,
"const auto &");
const FunctionDecl * Decl
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
llvm::Optional< Token > getQualifyingToken(tok::TokenKind TK, CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a CVR-qualified type, returns the token in Range that is responsible for th...
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)