Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:59

0001 //===- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This is a diagnostic client adaptor that performs rewrites as
0010 // suggested by code modification hints attached to diagnostics. It
0011 // then forwards any diagnostics to the adapted diagnostic client.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
0016 #define LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H
0017 
0018 #include "clang/Basic/Diagnostic.h"
0019 #include "clang/Basic/LLVM.h"
0020 #include "clang/Basic/SourceLocation.h"
0021 #include "clang/Edit/EditedSource.h"
0022 #include "clang/Rewrite/Core/Rewriter.h"
0023 #include <memory>
0024 #include <string>
0025 #include <utility>
0026 #include <vector>
0027 
0028 namespace clang {
0029 
0030 class LangOptions;
0031 class SourceManager;
0032 
0033 class FixItOptions {
0034 public:
0035   FixItOptions() = default;
0036   virtual ~FixItOptions();
0037 
0038   /// This file is about to be rewritten. Return the name of the file
0039   /// that is okay to write to.
0040   ///
0041   /// \param fd out parameter for file descriptor. After the call it may be set
0042   /// to an open file descriptor for the returned filename, or it will be -1
0043   /// otherwise.
0044   virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
0045 
0046   /// True if files should be updated in place. RewriteFilename is only called
0047   /// if this is false.
0048   bool InPlace = false;
0049 
0050   /// Whether to abort fixing a file when not all errors could be fixed.
0051   bool FixWhatYouCan = false;
0052 
0053   /// Whether to only fix warnings and not errors.
0054   bool FixOnlyWarnings = false;
0055 
0056   /// If true, only pass the diagnostic to the actual diagnostic consumer
0057   /// if it is an error or a fixit was applied as part of the diagnostic.
0058   /// It basically silences warnings without accompanying fixits.
0059   bool Silent = false;
0060 };
0061 
0062 class FixItRewriter : public DiagnosticConsumer {
0063   /// The diagnostics machinery.
0064   DiagnosticsEngine &Diags;
0065 
0066   edit::EditedSource Editor;
0067 
0068   /// The rewriter used to perform the various code
0069   /// modifications.
0070   Rewriter Rewrite;
0071 
0072   /// The diagnostic client that performs the actual formatting
0073   /// of error messages.
0074   DiagnosticConsumer *Client;
0075   std::unique_ptr<DiagnosticConsumer> Owner;
0076 
0077   /// Turn an input path into an output path. NULL implies overwriting
0078   /// the original.
0079   FixItOptions *FixItOpts;
0080 
0081   /// The number of rewriter failures.
0082   unsigned NumFailures = 0;
0083 
0084   /// Whether the previous diagnostic was not passed to the consumer.
0085   bool PrevDiagSilenced = false;
0086 
0087 public:
0088   /// Initialize a new fix-it rewriter.
0089   FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
0090                 const LangOptions &LangOpts, FixItOptions *FixItOpts);
0091 
0092   /// Destroy the fix-it rewriter.
0093   ~FixItRewriter() override;
0094 
0095   /// Check whether there are modifications for a given file.
0096   bool IsModified(FileID ID) const {
0097     return Rewrite.getRewriteBufferFor(ID) != nullptr;
0098   }
0099 
0100   using iterator = Rewriter::buffer_iterator;
0101 
0102   // Iteration over files with changes.
0103   iterator buffer_begin() { return Rewrite.buffer_begin(); }
0104   iterator buffer_end() { return Rewrite.buffer_end(); }
0105 
0106   /// Write a single modified source file.
0107   ///
0108   /// \returns true if there was an error, false otherwise.
0109   bool WriteFixedFile(FileID ID, raw_ostream &OS);
0110 
0111   /// Write the modified source files.
0112   ///
0113   /// \returns true if there was an error, false otherwise.
0114   bool WriteFixedFiles(
0115     std::vector<std::pair<std::string, std::string>> *RewrittenFiles = nullptr);
0116 
0117   /// IncludeInDiagnosticCounts - This method (whose default implementation
0118   /// returns true) indicates whether the diagnostics handled by this
0119   /// DiagnosticConsumer should be included in the number of diagnostics
0120   /// reported by DiagnosticsEngine.
0121   bool IncludeInDiagnosticCounts() const override;
0122 
0123   /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
0124   /// capturing it to a log as needed.
0125   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
0126                         const Diagnostic &Info) override;
0127 
0128   /// Emit a diagnostic via the adapted diagnostic client.
0129   void Diag(SourceLocation Loc, unsigned DiagID);
0130 };
0131 
0132 } // namespace clang
0133 
0134 #endif // LLVM_CLANG_REWRITE_FRONTEND_FIXITREWRITER_H