clang-tools  5.0.0
FixItHintUtils.cpp
Go to the documentation of this file.
1 //===--- FixItHintUtils.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 "FixItHintUtils.h"
11 #include "LexerUtils.h"
12 #include "clang/AST/ASTContext.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace utils {
17 namespace fixit {
18 
19 FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context) {
20  SourceLocation AmpLocation = Var.getLocation();
21  auto Token = utils::lexer::getPreviousToken(Context, AmpLocation);
22  if (!Token.is(tok::unknown))
23  AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
24  Context.getSourceManager(),
25  Context.getLangOpts());
26  return FixItHint::CreateInsertion(AmpLocation, "&");
27 }
28 
29 FixItHint changeVarDeclToConst(const VarDecl &Var) {
30  return FixItHint::CreateInsertion(Var.getTypeSpecStartLoc(), "const ");
31 }
32 
33 } // namespace fixit
34 } // namespace utils
35 } // namespace tidy
36 } // namespace clang
Token getPreviousToken(const ASTContext &Context, SourceLocation Location, bool SkipComments)
Returns previous token or tok::unknown if not found.
Definition: LexerUtils.cpp:17
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
FixItHint changeVarDeclToConst(const VarDecl &Var)
Creates fix to make VarDecl const qualified.
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.