LCOV - code coverage report
Current view: top level - clang/tools/extra/clang-tidy - ClangTidyDiagnosticConsumer.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 4 4 100.0 %
Date: 2018-05-20 00:06:23 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
      11             : #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
      12             : 
      13             : #include "ClangTidyOptions.h"
      14             : #include "clang/Basic/Diagnostic.h"
      15             : #include "clang/Basic/SourceManager.h"
      16             : #include "clang/Tooling/Core/Diagnostic.h"
      17             : #include "clang/Tooling/Refactoring.h"
      18             : #include "llvm/ADT/DenseMap.h"
      19             : #include "llvm/ADT/StringMap.h"
      20             : #include "llvm/Support/Regex.h"
      21             : #include "llvm/Support/Timer.h"
      22             : 
      23             : namespace clang {
      24             : 
      25             : class ASTContext;
      26             : class CompilerInstance;
      27             : namespace ast_matchers {
      28             : class MatchFinder;
      29             : }
      30             : namespace tooling {
      31             : class CompilationDatabase;
      32             : }
      33             : 
      34             : namespace tidy {
      35             : 
      36             : /// \brief A detected error complete with information to display diagnostic and
      37             : /// automatic fix.
      38             : ///
      39             : /// This is used as an intermediate format to transport Diagnostics without a
      40             : /// dependency on a SourceManager.
      41             : ///
      42             : /// FIXME: Make Diagnostics flexible enough to support this directly.
      43          94 : struct ClangTidyError : tooling::Diagnostic {
      44             :   ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory,
      45             :                  bool IsWarningAsError);
      46             : 
      47             :   bool IsWarningAsError;
      48             : };
      49             : 
      50             : /// \brief Read-only set of strings represented as a list of positive and
      51             : /// negative globs. Positive globs add all matched strings to the set, negative
      52             : /// globs remove them in the order of appearance in the list.
      53          32 : class GlobList {
      54             : public:
      55             :   /// \brief \p GlobList is a comma-separated list of globs (only '*'
      56             :   /// metacharacter is supported) with optional '-' prefix to denote exclusion.
      57             :   GlobList(StringRef Globs);
      58             : 
      59             :   /// \brief Returns \c true if the pattern matches \p S. The result is the last
      60             :   /// matching glob's Positive flag.
      61          30 :   bool contains(StringRef S) { return contains(S, false); }
      62             : 
      63             : private:
      64             :   bool contains(StringRef S, bool Contains);
      65             : 
      66             :   bool Positive;
      67             :   llvm::Regex Regex;
      68             :   std::unique_ptr<GlobList> NextGlob;
      69             : };
      70             : 
      71             : /// \brief Contains displayed and ignored diagnostic counters for a ClangTidy
      72             : /// run.
      73             : struct ClangTidyStats {
      74             :   ClangTidyStats()
      75             :       : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
      76             :         ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
      77             : 
      78             :   unsigned ErrorsDisplayed;
      79             :   unsigned ErrorsIgnoredCheckFilter;
      80             :   unsigned ErrorsIgnoredNOLINT;
      81             :   unsigned ErrorsIgnoredNonUserCode;
      82             :   unsigned ErrorsIgnoredLineFilter;
      83             : 
      84             :   unsigned errorsIgnored() const {
      85             :     return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
      86             :            ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
      87             :   }
      88             : };
      89             : 
      90             : /// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticsEngine
      91             : /// provided by this context.
      92             : ///
      93             : /// A \c ClangTidyCheck always has access to the active context to report
      94             : /// warnings like:
      95             : /// \code
      96             : /// Context->Diag(Loc, "Single-argument constructors must be explicit")
      97             : ///     << FixItHint::CreateInsertion(Loc, "explicit ");
      98             : /// \endcode
      99             : class ClangTidyContext {
     100             : public:
     101             :   /// \brief Initializes \c ClangTidyContext instance.
     102             :   ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
     103             :                    bool AllowEnablingAnalyzerAlphaCheckers = false);
     104             : 
     105             :   ~ClangTidyContext();
     106             : 
     107             :   /// \brief Report any errors detected using this method.
     108             :   ///
     109             :   /// This is still under heavy development and will likely change towards using
     110             :   /// tablegen'd diagnostic IDs.
     111             :   /// FIXME: Figure out a way to manage ID spaces.
     112             :   DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
     113             :                          StringRef Message,
     114             :                          DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
     115             : 
     116             :   /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
     117             :   ///
     118             :   /// This is called from the \c ClangTidyCheck base class.
     119             :   void setSourceManager(SourceManager *SourceMgr);
     120             : 
     121             :   /// \brief Should be called when starting to process new translation unit.
     122             :   void setCurrentFile(StringRef File);
     123             : 
     124             :   /// \brief Returns the main file name of the current translation unit.
     125             :   StringRef getCurrentFile() const { return CurrentFile; }
     126             : 
     127             :   /// \brief Sets ASTContext for the current translation unit.
     128             :   void setASTContext(ASTContext *Context);
     129             : 
     130             :   /// \brief Gets the language options from the AST context.
     131             :   const LangOptions &getLangOpts() const { return LangOpts; }
     132             : 
     133             :   /// \brief Returns the name of the clang-tidy check which produced this
     134             :   /// diagnostic ID.
     135             :   StringRef getCheckName(unsigned DiagnosticID) const;
     136             : 
     137             :   /// \brief Returns \c true if the check is enabled for the \c CurrentFile.
     138             :   ///
     139             :   /// The \c CurrentFile can be changed using \c setCurrentFile.
     140             :   bool isCheckEnabled(StringRef CheckName) const;
     141             : 
     142             :   /// \brief Returns \c true if the check should be upgraded to error for the
     143             :   /// \c CurrentFile.
     144             :   bool treatAsError(StringRef CheckName) const;
     145             : 
     146             :   /// \brief Returns global options.
     147             :   const ClangTidyGlobalOptions &getGlobalOptions() const;
     148             : 
     149             :   /// \brief Returns options for \c CurrentFile.
     150             :   ///
     151             :   /// The \c CurrentFile can be changed using \c setCurrentFile.
     152             :   const ClangTidyOptions &getOptions() const;
     153             : 
     154             :   /// \brief Returns options for \c File. Does not change or depend on
     155             :   /// \c CurrentFile.
     156             :   ClangTidyOptions getOptionsForFile(StringRef File) const;
     157             : 
     158             :   /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
     159             :   /// counters.
     160             :   const ClangTidyStats &getStats() const { return Stats; }
     161             : 
     162             :   /// \brief Returns all collected errors.
     163             :   ArrayRef<ClangTidyError> getErrors() const { return Errors; }
     164             : 
     165             :   /// \brief Clears collected errors.
     166             :   void clearErrors() { Errors.clear(); }
     167             : 
     168             :   /// \brief Control profile collection in clang-tidy.
     169             :   void setEnableProfiling(bool Profile);
     170             :   bool getEnableProfiling() const { return Profile; }
     171             : 
     172             :   /// \brief Should be called when starting to process new translation unit.
     173             :   void setCurrentBuildDirectory(StringRef BuildDirectory) {
     174             :     CurrentBuildDirectory = BuildDirectory;
     175             :   }
     176             : 
     177             :   /// \brief Returns build directory of the current translation unit.
     178             :   const std::string &getCurrentBuildDirectory() {
     179             :     return CurrentBuildDirectory;
     180             :   }
     181             : 
     182             :   /// \brief If the experimental alpha checkers from the static analyzer can be
     183             :   /// enabled.
     184             :   bool canEnableAnalyzerAlphaCheckers() const {
     185             :     return AllowEnablingAnalyzerAlphaCheckers;
     186             :   }
     187             : 
     188             : private:
     189             :   // Calls setDiagnosticsEngine() and storeError().
     190             :   friend class ClangTidyDiagnosticConsumer;
     191             :   friend class ClangTidyPluginAction;
     192             : 
     193             :   /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
     194             :   /// correctly.
     195             :   void setDiagnosticsEngine(DiagnosticsEngine *Engine);
     196             : 
     197             :   /// \brief Store an \p Error.
     198             :   void storeError(const ClangTidyError &Error);
     199             : 
     200             :   std::vector<ClangTidyError> Errors;
     201             :   DiagnosticsEngine *DiagEngine;
     202             :   std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
     203             : 
     204             :   std::string CurrentFile;
     205             :   ClangTidyOptions CurrentOptions;
     206             :   class CachedGlobList;
     207             :   std::unique_ptr<CachedGlobList> CheckFilter;
     208             :   std::unique_ptr<CachedGlobList> WarningAsErrorFilter;
     209             : 
     210             :   LangOptions LangOpts;
     211             : 
     212             :   ClangTidyStats Stats;
     213             : 
     214             :   std::string CurrentBuildDirectory;
     215             : 
     216             :   llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
     217             : 
     218             :   bool Profile;
     219             : 
     220             :   bool AllowEnablingAnalyzerAlphaCheckers;
     221             : };
     222             : 
     223             : /// \brief A diagnostic consumer that turns each \c Diagnostic into a
     224             : /// \c SourceManager-independent \c ClangTidyError.
     225             : //
     226             : // FIXME: If we move away from unit-tests, this can be moved to a private
     227             : // implementation file.
     228         133 : class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
     229             : public:
     230             :   ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx,
     231             :                               bool RemoveIncompatibleErrors = true);
     232             : 
     233             :   // FIXME: The concept of converting between FixItHints and Replacements is
     234             :   // more generic and should be pulled out into a more useful Diagnostics
     235             :   // library.
     236             :   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
     237             :                         const Diagnostic &Info) override;
     238             : 
     239             :   /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext.
     240             :   void finish() override;
     241             : 
     242             : private:
     243             :   void finalizeLastError();
     244             : 
     245             :   void removeIncompatibleErrors(SmallVectorImpl<ClangTidyError> &Errors) const;
     246             : 
     247             :   /// \brief Returns the \c HeaderFilter constructed for the options set in the
     248             :   /// context.
     249             :   llvm::Regex *getHeaderFilter();
     250             : 
     251             :   /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
     252             :   /// according to the diagnostic \p Location.
     253             :   void checkFilters(SourceLocation Location);
     254             :   bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
     255             : 
     256             :   ClangTidyContext &Context;
     257             :   bool RemoveIncompatibleErrors;
     258             :   std::unique_ptr<DiagnosticsEngine> Diags;
     259             :   SmallVector<ClangTidyError, 8> Errors;
     260             :   std::unique_ptr<llvm::Regex> HeaderFilter;
     261             :   bool LastErrorRelatesToUserCode;
     262             :   bool LastErrorPassesLineFilter;
     263             :   bool LastErrorWasIgnored;
     264             : };
     265             : 
     266             : } // end namespace tidy
     267             : } // end namespace clang
     268             : 
     269             : #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H

Generated by: LCOV version 1.13