// Re-lex the tokens to get precise locations to insert 'override' and remove
// 'virtual'.
-static SmallVector<Token, 16> ParseTokens(CharSourceRange Range,
- const SourceManager &Sources,
- LangOptions LangOpts) {
+static SmallVector<Token, 16>
+ParseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
+ const SourceManager &Sources = *Result.SourceManager;
std::pair<FileID, unsigned> LocInfo =
Sources.getDecomposedLoc(Range.getBegin());
StringRef File = Sources.getBufferData(LocInfo.first);
const char *TokenBegin = File.data() + LocInfo.second;
- Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first), LangOpts,
- File.begin(), TokenBegin, File.end());
+ Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
+ Result.Context->getLangOpts(), File.begin(), TokenBegin,
+ File.end());
SmallVector<Token, 16> Tokens;
Token Tok;
while (!RawLexer.LexFromRawLexer(Tok)) {
break;
if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
break;
+ if (Tok.is(tok::raw_identifier)) {
+ IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
+ Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
+ Tok.setIdentifierInfo(&Info);
+ Tok.setKind(Info.getTokenID());
+ }
Tokens.push_back(Tok);
}
return Tokens;
// FIXME: Instead of re-lexing and looking for specific macros such as
// 'ABSTRACT', properly store the location of 'virtual' and '= 0' in each
// FunctionDecl.
- SmallVector<Token, 16> Tokens = ParseTokens(FileRange, Sources,
- Result.Context->getLangOpts());
+ SmallVector<Token, 16> Tokens = ParseTokens(FileRange, Result);
// Add 'override' on inline declarations that don't already have it.
if (!HasFinal && !HasOverride) {
SourceLocation InsertLoc;
StringRef ReplacementText = "override ";
+ for (Token T : Tokens) {
+ if (T.is(tok::kw___attribute)) {
+ InsertLoc = T.getLocation();
+ break;
+ }
+ }
+
if (Method->hasAttrs()) {
for (const clang::Attr *A : Method->getAttrs()) {
if (!A->isImplicit()) {
- SourceLocation Loc = Sources.getExpansionLoc(A->getLocation());
+ SourceLocation Loc =
+ Sources.getExpansionLoc(A->getRange().getBegin());
if (!InsertLoc.isValid() ||
Sources.isBeforeInTranslationUnit(Loc, InsertLoc))
InsertLoc = Loc;
if (Method->isVirtualAsWritten()) {
for (Token Tok : Tokens) {
- if (Tok.is(tok::raw_identifier) && GetText(Tok, Sources) == "virtual") {
+ if (Tok.is(tok::kw_virtual)) {
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
Tok.getLocation(), Tok.getLocation()));
break;