daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "clang/Frontend/FrontendPluginRegistry.h"
  2. #include "clang/AST/ASTConsumer.h"
  3. #include "clang/AST/AST.h"
  4. #include "clang/Frontend/CompilerInstance.h"
  5. #include "llvm/Support/raw_ostream.h"
  6. using namespace clang;
  7.  
  8. namespace {
  9.  
  10. class OverrideConsumer : public ASTConsumer {
  11. public:
  12.   virtual void HandleTagDeclDefinition(TagDecl* tag) {
  13.     if (CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)) {
  14.       for (CXXRecordDecl::method_iterator method = record->method_begin();
  15.            method != record->method_end(); ++method) {
  16.         if (method->isVirtual()) {
  17.           bool has_override_attr = method->getAttr<OverrideAttr>();
  18.           bool is_override = method->begin_overridden_methods() != method->end_overridden_methods();
  19.  
  20.           if (is_override && !has_override_attr) {
  21.             Diagnostic& D = method->getASTContext().getDiagnostics();
  22.             unsigned id = D.getCustomDiagID(Diagnostic::Warning,
  23.                 "method overrides parent class; consider adding OVERRIDE");
  24.             D.Report(method->getASTContext().getFullLoc(method->getLocation()), id);
  25.           } else if (has_override_attr && !is_override) {
  26.             Diagnostic& D = method->getASTContext().getDiagnostics();
  27.             unsigned id = D.getCustomDiagID(Diagnostic::Error,
  28.                 "method claims OVERRIDE but fails to override any superclass method");
  29.             D.Report(method->getASTContext().getFullLoc(method->getLocation()), id);
  30.           }
  31.         }
  32.       }
  33.     }
  34.   }
  35. };
  36.  
  37. class CheckOverrideAction : public PluginASTAction {
  38. protected:
  39.   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
  40.     return new OverrideConsumer();
  41.   }
  42.  
  43.   bool ParseArgs(const CompilerInstance &CI,
  44.                  const std::vector<std::string>& args) {
  45.     return true;
  46.   }
  47. };
  48.  
  49. }
  50.  
  51. static FrontendPluginRegistry::Add<CheckOverrideAction>
  52. X("check-overrides", "check usage of __attribute__((override))");
RAW Paste Data
Top