From b8c38756180df345534d4c816f648a9d74760f42 Mon Sep 17 00:00:00 2001 From: Edwin Vane Date: Fri, 10 May 2013 14:04:58 +0000 Subject: cpp11-migrate: Fix crash in AddOverride due to template instantiations This patch fixes different issues: - override is not added in template 'contexts' (this will be further improved to handle safe situations, a test for this has been written already) - the main file is now checked before the modifications are applied - override is not applied now when dealing with pure methods since it was misplaced (ignoring it isn't the perfect solution but it seems difficult to find the location just before the pure-specifier) Fixes PR15827 Author: Guillaume Papin llvm-svn: 181596 --- .../AddOverride/AddOverrideActions.cpp | 56 +++++++++++++--------- 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp') diff --git a/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp b/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp index 3d1f1ff8c13..197befa4f4f 100644 --- a/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp +++ b/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp @@ -36,28 +36,40 @@ void AddOverrideFixer::run(const MatchFinder::MatchResult &Result) { if (!SM.isFromMainFile(M->getLocStart())) return; + if (!SM.isFromMainFile(M->getLocStart())) + return; + // First check that there isn't already an override attribute. - if (!M->hasAttr()) { - if (M->getLocStart().isFileID()) { - SourceLocation StartLoc; - if (M->hasInlineBody()) { - // Start at the beginning of the body and rewind back to the last - // non-whitespace character. We will insert the override keyword - // after that character. - // FIXME: This transform won't work if there is a comment between - // the end of the function prototype and the start of the body. - StartLoc = M->getBody()->getLocStart(); - do { - StartLoc = StartLoc.getLocWithOffset(-1); - } while (isWhitespace(*FullSourceLoc(StartLoc, SM).getCharacterData())); - StartLoc = StartLoc.getLocWithOffset(1); - } else { - StartLoc = SM.getSpellingLoc(M->getLocEnd()); - StartLoc = Lexer::getLocForEndOfToken(StartLoc, 0, SM, LangOptions()); - } - Replace.insert(tooling::Replacement(SM, StartLoc, 0, " override")); - ++AcceptedChanges; - } + if (M->hasAttr()) + return; + + // FIXME: Pure methods are not supported yet as it is difficult to track down + // the location of '= 0'. + if (M->isPure()) + return; + + if (const FunctionDecl *TemplateMethod = M->getTemplateInstantiationPattern()) + M = cast(TemplateMethod); + + if (M->getParent()->hasAnyDependentBases()) + return; + + SourceLocation StartLoc; + if (M->hasInlineBody()) { + // Start at the beginning of the body and rewind back to the last + // non-whitespace character. We will insert the override keyword + // after that character. + // FIXME: This transform won't work if there is a comment between + // the end of the function prototype and the start of the body. + StartLoc = M->getBody()->getLocStart(); + do { + StartLoc = StartLoc.getLocWithOffset(-1); + } while (isWhitespace(*FullSourceLoc(StartLoc, SM).getCharacterData())); + StartLoc = StartLoc.getLocWithOffset(1); + } else { + StartLoc = SM.getSpellingLoc(M->getLocEnd()); + StartLoc = Lexer::getLocForEndOfToken(StartLoc, 0, SM, LangOptions()); } + Replace.insert(tooling::Replacement(SM, StartLoc, 0, " override")); + ++AcceptedChanges; } - -- cgit v1.2.3