Index: tools/Makefile
===================================================================
--- tools/Makefile	(revision 160218)
+++ tools/Makefile	(working copy)
@@ -9,7 +9,7 @@
 
 CLANG_LEVEL := ..
 DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool \
-        clang-check remove-cstr-calls ast-query
+        clang-check remove-cstr-calls ast-query add-override-specifier
 
 include $(CLANG_LEVEL)/../../Makefile.config
 
Index: tools/CMakeLists.txt
===================================================================
--- tools/CMakeLists.txt	(revision 160218)
+++ tools/CMakeLists.txt	(working copy)
@@ -9,3 +9,4 @@
 add_subdirectory(remove-cstr-calls)
 add_subdirectory(fix-llvm-style)
 add_subdirectory(rename)
+add_subdirectory(add-override-specifier)
Index: tools/add-override-specifier/Makefile
===================================================================
--- tools/add-override-specifier/Makefile	(revision 0)
+++ tools/add-override-specifier/Makefile	(revision 0)
@@ -0,0 +1,23 @@
+##===- examples/add-override-specifier/Makefile ------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL := ../..
+
+TOOLNAME = add-override-specifier
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+LINK_COMPONENTS := support mc
+USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \
+           clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \
+           clangEdit.a clangAST.a clangASTMatchers.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile
Index: tools/add-override-specifier/CMakeLists.txt
===================================================================
--- tools/add-override-specifier/CMakeLists.txt	(revision 0)
+++ tools/add-override-specifier/CMakeLists.txt	(revision 0)
@@ -0,0 +1,12 @@
+set(LLVM_USED_LIBS clangTooling clangEdit clangBasic clangAST clangASTMatchers)
+
+add_clang_executable(add-override-specifier
+  AddOverrideSpecifier.cpp
+  )
+
+target_link_libraries(cxxmigrate
+  clangTooling
+  clangEdit
+  clangBasic
+  clangAST
+  clangASTMatchers)
Index: tools/add-override-specifier/AddOverrideSpecifier.cpp
===================================================================
--- tools/add-override-specifier/AddOverrideSpecifier.cpp	(revision 0)
+++ tools/add-override-specifier/AddOverrideSpecifier.cpp	(revision 0)
@@ -0,0 +1,145 @@
+//=- examples/add-override-specifier/AddOverrideSpecifier.cpp -------------===//
+//
+//           The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file implements a tool that modifies the input files to add
+//  the override specifier to virtual functions where it can be, ie.
+//  functions that override virtual functions but are not already
+//  tagged with override.
+//
+//  Usage:
+//  add-override-specifier <cmake-output-dir> <file1> [<file2> ...]
+//
+//  Where <cmake-output-dir> is a CMake build directory in which a file named
+//  compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
+//  CMake to get this output).
+//
+//  <file1> ... specify the paths of files in the CMake source tree.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Lex/Preprocessor.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Refactoring.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace clang;
+using namespace clang::ast_matchers;
+using namespace clang::tooling;
+using namespace llvm;
+
+cl::opt<std::string> BuildPath(
+  cl::Positional,
+  cl::desc("<build-path>"));
+
+cl::list<std::string> SourcePaths(
+  cl::Positional,
+  cl::desc("<source0> [... <sourceN>]"),
+  cl::OneOrMore);
+
+namespace clang {
+  namespace ast_matchers {
+    // Identify methods that overload virtual methods from the base class
+    // but do not already have the override attribute
+    // Given
+    //   class X { virtual void f(int x) {} };
+    //   class Y { virtual void f(int x) {} };
+    //   class Z { virtual void f(int x) const {}
+    //       virtual void f(int x) override {} };
+    // Method(isOverrideMissing())
+    //   matches Y::f(int x) {}
+    //   but not Z:f(int) const because it does not override a virtual function
+    //   nor Z::f(int) override
+    AST_MATCHER(clang::CXXMethodDecl, isOverrideMissing) {
+      bool IsCanonicalDecl = Node.isCanonicalDecl();
+      bool VirtualMethod = Node.isVirtual();
+      bool IsDestructor = llvm::isa<CXXDestructorDecl>(Node);
+      bool OverridesMethods = (Node.size_overridden_methods() > 0);
+      bool HasOverrideAttr = Node.hasAttr<OverrideAttr>();
+      return IsCanonicalDecl && VirtualMethod && !IsDestructor &&
+             OverridesMethods && !HasOverrideAttr;
+    }
+  } // namespace ast_matchers
+} // namespace clang
+
+static bool isWhitespace(char C) {
+  return (C == ' ') || (C == '\t') || (C == '\f') ||
+         (C == '\v') || (C == '\n') || (C == '\r');
+}
+
+// Adds the override keyword to the end of the virtual function declaration.
+// Assumes that it is not already there.
+class AddOverrideAttributeCallback : public MatchFinder::MatchCallback {
+public:
+  AddOverrideAttributeCallback(Replacements *Replace) : Replace(Replace) {}
+
+  virtual void run(const MatchFinder::MatchResult &Result) {
+    SourceManager& SourceManager = *Result.SourceManager;
+    const CXXMethodDecl* M = Result.Nodes.getDeclAs<CXXMethodDecl>("method");
+
+    if (M->getLocStart().isFileID()) {
+      SourceLocation Loc;
+      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.
+        // This won't work if there is a comment between the end of the
+        // function prototype and the start of the body.
+        Loc = M->getBody()->getLocStart();
+        do {
+          Loc = Loc.getLocWithOffset(-1);
+        } while (isWhitespace(*FullSourceLoc(Loc, SourceManager).
+                     getCharacterData()));
+        Loc = Loc.getLocWithOffset(1);
+      }
+      else {
+        Loc = SourceManager.getSpellingLoc(M->getLocEnd());
+        Loc = Lexer::getLocForEndOfToken(Loc, 0, SourceManager, LangOptions());
+      }
+      Replace->insert(Replacement(SourceManager, Loc, 0, " override"));
+    }
+  }
+
+private:
+  Replacements *Replace;
+};
+
+int main(int argc, const char **argv) {
+  // First see if we can create the compile command line from the
+  // positional parameters after "--".
+  OwningPtr<CompilationDatabase> Compilations(
+  FixedCompilationDatabase::loadFromCommandLine(argc, argv));
+
+  // Do normal command line handling from the rest of the arguments.
+  cl::ParseCommandLineOptions(argc, argv);
+
+  if (!Compilations) {
+    // If the caller didn't specify a compile command line to use, try to
+    // load it from a build directory. For example when running cmake, use
+    // CMAKE_EXPORT_COMPILE_COMMANDS=ON to prepare your build directory to
+    // be useable with clang tools.
+    std::string ErrorMessage;
+    Compilations.reset(CompilationDatabase::loadFromDirectory(BuildPath,
+                                  ErrorMessage));
+    if (!Compilations)
+      llvm::report_fatal_error(ErrorMessage);
+  }
+
+  RefactoringTool Tool(*Compilations, SourcePaths);
+  ast_matchers::MatchFinder Finder;
+  AddOverrideAttributeCallback callback(&Tool.getReplacements());
+  Finder.addMatcher(
+    // Match method calls where the method can be marked override
+    id("method", method(isOverrideMissing())),
+    &callback);
+
+  return Tool.run(newFrontendActionFactory(&Finder));
+}
+
Index: test/Tooling/add-override-specifier.cpp
===================================================================
--- test/Tooling/add-override-specifier.cpp	(revision 0)
+++ test/Tooling/add-override-specifier.cpp	(revision 0)
@@ -0,0 +1,103 @@
+// RUN: rm -rf %t.cpp
+// RUN: grep -Ev "//\s*[A-Z-]+:" %s > %t.cpp
+// RUN: add-override-specifier %t.cpp -- -std=c++11
+// RUN: cat "%t.cpp" | FileCheck %s
+// RUN: rm -rf %t.cpp
+
+class A
+{
+public:
+    virtual ~A();
+    // CHECK: virtual ~A();
+    void f();
+    virtual void h() const;
+    virtual void i() = 0;
+};
+
+// test that override isn't added to non-virtual functions
+class B : public A
+{
+public:
+    void f();
+    // CHECK: class B
+    // CHECK: void f();
+};
+
+// test that override is added to functions that override virtual functions
+class C : public A
+{
+public:
+    void h() const;
+    // CHECK: class C
+    // CHECK: void h() const override;
+};
+
+// test that override isn't add to functions that overload but not override
+class D : public A
+{
+public:
+    void h();
+    // CHECK: class D
+    // CHECK: void h();
+};
+
+// test that override isn't added again to functions that already have it
+class E : public A
+{
+public:
+    void h() const override;
+    // CHECK: class E
+    // CHECK: void h() const override;
+};
+
+// test that override isn't added to the destructor
+class F : public A
+{
+public:
+    virtual ~F();
+    // CHECK: class F
+    // CHECK: virtual ~F();
+};
+
+// check that override is placed before any end of line comments
+class G : public A
+{
+public:
+    void h() const; // comment
+    // CHECK: class G
+    // CHECK: void h() const override; // comment
+};
+
+// check that override is placed correctly if there is an inline body
+class H : public A
+{
+public:
+    void h() const {}
+    // CHECK: class H
+    // CHECK: void h() const override {}
+};
+
+// check that override is placed correctly if there is a body on the next line
+class I : public A
+{
+public:
+    void h() const
+    {}
+    // CHECK: class I
+    // CHECK: void h() const override
+};
+
+// check that override is placed correctly if there is a body outside the class
+class J : public A
+{
+public:
+    void h() const;
+    // CHECK: class J
+    // CHECK: void h() const override;
+};
+
+void J::h() const
+{
+    // CHECK: void J::h() const
+}
+
