summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/AddOverride/AddOverride.cpp
blob: fdfde5f2e37a05bce02bacd6c3071c9ea2558708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===-- AddOverride/AddOverride.cpp - add C++11 override ------------------===//
//
//                     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 provides the implementation of the AddOverrideTransform
/// class.
///
//===----------------------------------------------------------------------===//

#include "AddOverride.h"
#include "AddOverrideActions.h"
#include "AddOverrideMatchers.h"
#include "clang/Frontend/CompilerInstance.h"

using clang::ast_matchers::MatchFinder;
using namespace clang::tooling;
using namespace clang;
namespace cl = llvm::cl;

static cl::opt<bool> DetectMacros(
    "override-macros",
    cl::desc("Detect and use macros that expand to the 'override' keyword."),
    cl::cat(TransformsOptionsCategory));

int AddOverrideTransform::apply(const CompilationDatabase &Database,
                                const std::vector<std::string> &SourcePaths) {
  ClangTool AddOverrideTool(Database, SourcePaths);
  unsigned AcceptedChanges = 0;
  MatchFinder Finder;
  AddOverrideFixer Fixer(AcceptedChanges, DetectMacros,
                         /*Owner=*/ *this);
  Finder.addMatcher(makeCandidateForOverrideAttrMatcher(), &Fixer);

  // Make Fixer available to handleBeginSource().
  this->Fixer = &Fixer;

  if (int result = AddOverrideTool.run(createActionFactory(Finder).get())) {
    llvm::errs() << "Error encountered during translation.\n";
    return result;
  }

  setAcceptedChanges(AcceptedChanges);
  return 0;
}

bool AddOverrideTransform::handleBeginSource(clang::CompilerInstance &CI,
                                             llvm::StringRef Filename) {
  assert(Fixer != nullptr && "Fixer must be set");
  Fixer->setPreprocessor(CI.getPreprocessor());
  return Transform::handleBeginSource(CI, Filename);
}

namespace {
struct AddOverrideFactory : TransformFactory {
  AddOverrideFactory() {
    // if detecting macros is enabled, do not impose requirements on the
    // compiler. It is assumed that the macros use is "C++11-aware", meaning it
    // won't expand to override if the compiler doesn't support the specifier.
    if (!DetectMacros) {
      Since.Clang = Version(3, 0);
      Since.Gcc = Version(4, 7);
      Since.Icc = Version(14);
      Since.Msvc = Version(8);
    }
  }

  Transform *createTransform(const TransformOptions &Opts) override {
    return new AddOverrideTransform(Opts);
  }
};
} // namespace

// Register the factory using this statically initialized variable.
static TransformFactoryRegistry::Add<AddOverrideFactory>
X("add-override", "Make use of override specifier where possible");

// This anchor is used to force the linker to link in the generated object file
// and thus register the factory.
volatile int AddOverrideTransformAnchorSource = 0;
OpenPOWER on IntegriCloud