summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp')
-rw-r--r--clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp b/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp
new file mode 100644
index 00000000000..a40f673132c
--- /dev/null
+++ b/clang-tools-extra/cpp11-migrate/AddOverride/AddOverrideActions.cpp
@@ -0,0 +1,59 @@
+//===-- AddOverride/AddOverrideActions.cpp - add C++11 override-*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// \brief This file contains the definition of the AddOverrideFixer class
+/// which is used as an ASTMatcher callback.
+///
+//===----------------------------------------------------------------------===//
+
+#include "AddOverrideActions.h"
+#include "AddOverrideMatchers.h"
+
+#include "clang/Basic/CharInfo.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Attr.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+using namespace clang::tooling;
+using namespace clang;
+
+void AddOverrideFixer::run(const MatchFinder::MatchResult &Result) {
+ SourceManager &SM = *Result.SourceManager;
+
+ const CXXMethodDecl *M = Result.Nodes.getDeclAs<CXXMethodDecl>(MethodId);
+ assert(M && "Bad Callback. No node provided");
+
+ // First check that there isn't already an override attribute.
+ if (!M->hasAttr<OverrideAttr>()) {
+ 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;
+ }
+ }
+}
+
OpenPOWER on IntegriCloud