ChromeClassTester.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_
  5. #define TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_
  6. #include <set>
  7. #include <vector>
  8. #include "Options.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/TypeLoc.h"
  11. #include "clang/Frontend/CompilerInstance.h"
  12. // A class on top of ASTConsumer that forwards classes defined in Chromium
  13. // headers to subclasses which implement CheckChromeClass().
  14. // TODO(vmpstr): Fold this class into FindBadConstructsConsumer.
  15. class ChromeClassTester {
  16. public:
  17. ChromeClassTester(clang::CompilerInstance& instance,
  18. const chrome_checker::Options& options);
  19. virtual ~ChromeClassTester();
  20. void CheckTag(clang::TagDecl*);
  21. clang::DiagnosticsEngine::Level getErrorLevel();
  22. protected:
  23. clang::CompilerInstance& instance() { return instance_; }
  24. clang::DiagnosticsEngine& diagnostic() { return diagnostic_; }
  25. // Utility method for subclasses to check how a certain SourceLocation should
  26. // be handled. The main criteria for classification is the SourceLocation's
  27. // path (e.g. whether it's in //third_party).
  28. enum class LocationType {
  29. // Enforce all default checks.
  30. kChrome,
  31. // Enforces a subset of checks for Blink code. This is hopefully a
  32. // transitional stage, as more plugin checks are gradually enabled in Blink.
  33. kBlink,
  34. // Skip all checks. Typically, this is third-party or generated code where
  35. // it doesn't make sense to enforce Chrome's custom diagnostics.
  36. kThirdParty,
  37. };
  38. LocationType ClassifyLocation(clang::SourceLocation loc);
  39. // Utility method to check whether the given record has any of the ignored
  40. // base classes.
  41. bool HasIgnoredBases(const clang::CXXRecordDecl* record);
  42. // Utility method for subclasses to check if this class is within an
  43. // implementation (.cc, .cpp, .mm) file.
  44. bool InImplementationFile(clang::SourceLocation location);
  45. // Options.
  46. const chrome_checker::Options options_;
  47. private:
  48. void BuildBannedLists();
  49. // Filtered versions of tags that are only called with things defined in
  50. // chrome header files.
  51. virtual void CheckChromeClass(LocationType location_type,
  52. clang::SourceLocation record_location,
  53. clang::CXXRecordDecl* record) = 0;
  54. // Utility methods used for filtering out non-chrome classes (and ones we
  55. // deliberately ignore) in HandleTagDeclDefinition().
  56. bool IsIgnoredType(const std::string& base_name);
  57. // Attempts to determine the filename for the given SourceLocation.
  58. // Returns false if the filename could not be determined.
  59. bool GetFilename(clang::SourceLocation loc, std::string* filename);
  60. clang::CompilerInstance& instance_;
  61. clang::DiagnosticsEngine& diagnostic_;
  62. // List of banned directories.
  63. std::set<std::string> banned_directories_;
  64. // List of types that we don't check.
  65. std::set<std::string> ignored_record_names_;
  66. // List of base classes that we skip when checking complex class ctors/dtors.
  67. std::set<std::string> ignored_base_classes_;
  68. };
  69. #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_