12
12
#include " clang/AST/RecursiveASTVisitor.h"
13
13
#include " clang/AST/StmtVisitor.h"
14
14
#include " clang/ASTMatchers/ASTMatchFinder.h"
15
+ #include " clang/Basic/CharInfo.h"
16
+ #include " clang/Basic/SourceLocation.h"
15
17
#include " clang/Lex/Lexer.h"
16
18
#include " clang/Lex/Preprocessor.h"
17
19
#include " llvm/ADT/APSInt.h"
@@ -2492,8 +2494,25 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
2492
2494
return {};
2493
2495
}
2494
2496
2497
+ const SourceLocation IdentifierLoc = getVarDeclIdentifierLoc (D);
2498
+
2499
+ // Get the spelling of the element type as written in the source file (including macros, etc.).
2500
+ auto MaybeElemTypeTxt = getRangeText ({D->getBeginLoc (), IdentifierLoc}, Ctx.getSourceManager (), Ctx.getLangOpts ());
2501
+ if (!MaybeElemTypeTxt)
2502
+ return {};
2503
+ std::string ElemTypeTxt = MaybeElemTypeTxt->str ();
2504
+ // Trim whitespace from the type spelling.
2505
+ unsigned TrailingWhitespace = 0 ;
2506
+ for (auto It = ElemTypeTxt.rbegin (); It < ElemTypeTxt.rend (); ++It) {
2507
+ if (!isWhitespace (*It))
2508
+ break ;
2509
+ ++TrailingWhitespace;
2510
+ }
2511
+ if (TrailingWhitespace > 0 )
2512
+ ElemTypeTxt.erase (ElemTypeTxt.length () - TrailingWhitespace);
2513
+
2495
2514
// Find the '[' token.
2496
- std::optional<Token> NextTok = Lexer::findNextToken (getVarDeclIdentifierLoc (D) , Ctx.getSourceManager (), Ctx.getLangOpts ());
2515
+ std::optional<Token> NextTok = Lexer::findNextToken (IdentifierLoc , Ctx.getSourceManager (), Ctx.getLangOpts ());
2497
2516
while (NextTok && !NextTok->is (tok::l_square))
2498
2517
NextTok = Lexer::findNextToken (NextTok->getLocation (), Ctx.getSourceManager (), Ctx.getLangOpts ());
2499
2518
if (!NextTok)
@@ -2517,7 +2536,7 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
2517
2536
2518
2537
SmallString<32 > Replacement;
2519
2538
raw_svector_ostream OS (Replacement);
2520
- OS << " std::array<" << ArrayEltT. getAsString () << " , "
2539
+ OS << " std::array<" << ElemTypeTxt << " , "
2521
2540
<< ArraySizeTxt << " > " << IdentText->str ();
2522
2541
2523
2542
FixIts.push_back (FixItHint::CreateReplacement (
0 commit comments