diff options
Diffstat (limited to 'include/clang/Rewrite/Frontend')
-rw-r--r-- | include/clang/Rewrite/Frontend/ASTConsumers.h | 48 | ||||
-rw-r--r-- | include/clang/Rewrite/Frontend/FixItRewriter.h | 130 | ||||
-rw-r--r-- | include/clang/Rewrite/Frontend/FrontendActions.h | 83 | ||||
-rw-r--r-- | include/clang/Rewrite/Frontend/Rewriters.h | 35 |
4 files changed, 296 insertions, 0 deletions
diff --git a/include/clang/Rewrite/Frontend/ASTConsumers.h b/include/clang/Rewrite/Frontend/ASTConsumers.h new file mode 100644 index 0000000..c9c92e3 --- /dev/null +++ b/include/clang/Rewrite/Frontend/ASTConsumers.h @@ -0,0 +1,48 @@ +//===--- ASTConsumers.h - ASTConsumer implementations -----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// AST Consumers. +// +//===----------------------------------------------------------------------===// + +#ifndef REWRITE_ASTCONSUMERS_H +#define REWRITE_ASTCONSUMERS_H + +#include "clang/Basic/LLVM.h" +#include <string> + +namespace clang { + +class ASTConsumer; +class DiagnosticsEngine; +class LangOptions; +class Preprocessor; + +// ObjC rewriter: attempts to rewrite ObjC constructs into pure C code. +// This is considered experimental, and only works with Apple's ObjC runtime. +ASTConsumer *CreateObjCRewriter(const std::string &InFile, + raw_ostream *OS, + DiagnosticsEngine &Diags, + const LangOptions &LOpts, + bool SilenceRewriteMacroWarning); +ASTConsumer *CreateModernObjCRewriter(const std::string &InFile, + raw_ostream *OS, + DiagnosticsEngine &Diags, + const LangOptions &LOpts, + bool SilenceRewriteMacroWarning); + +/// CreateHTMLPrinter - Create an AST consumer which rewrites source code to +/// HTML with syntax highlighting suitable for viewing in a web-browser. +ASTConsumer *CreateHTMLPrinter(raw_ostream *OS, Preprocessor &PP, + bool SyntaxHighlight = true, + bool HighlightMacros = true); + +} // end clang namespace + +#endif diff --git a/include/clang/Rewrite/Frontend/FixItRewriter.h b/include/clang/Rewrite/Frontend/FixItRewriter.h new file mode 100644 index 0000000..f12a034 --- /dev/null +++ b/include/clang/Rewrite/Frontend/FixItRewriter.h @@ -0,0 +1,130 @@ +//===--- FixItRewriter.h - Fix-It Rewriter Diagnostic Client ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This is a diagnostic client adaptor that performs rewrites as +// suggested by code modification hints attached to diagnostics. It +// then forwards any diagnostics to the adapted diagnostic client. +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H +#define LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Rewrite/Core/Rewriter.h" +#include "clang/Edit/EditedSource.h" + +namespace clang { + +class SourceManager; +class FileEntry; + +class FixItOptions { +public: + FixItOptions() : FixWhatYouCan(false), + FixOnlyWarnings(false), Silent(false) { } + + virtual ~FixItOptions(); + + /// \brief This file is about to be rewritten. Return the name of the file + /// that is okay to write to. + /// + /// \param fd out parameter for file descriptor. After the call it may be set + /// to an open file descriptor for the returned filename, or it will be -1 + /// otherwise. + /// + virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0; + + /// \brief Whether to abort fixing a file when not all errors could be fixed. + bool FixWhatYouCan; + + /// \brief Whether to only fix warnings and not errors. + bool FixOnlyWarnings; + + /// \brief If true, only pass the diagnostic to the actual diagnostic consumer + /// if it is an error or a fixit was applied as part of the diagnostic. + /// It basically silences warnings without accompanying fixits. + bool Silent; +}; + +class FixItRewriter : public DiagnosticConsumer { + /// \brief The diagnostics machinery. + DiagnosticsEngine &Diags; + + edit::EditedSource Editor; + + /// \brief The rewriter used to perform the various code + /// modifications. + Rewriter Rewrite; + + /// \brief The diagnostic client that performs the actual formatting + /// of error messages. + DiagnosticConsumer *Client; + bool OwnsClient; + + /// \brief Turn an input path into an output path. NULL implies overwriting + /// the original. + FixItOptions *FixItOpts; + + /// \brief The number of rewriter failures. + unsigned NumFailures; + + /// \brief Whether the previous diagnostic was not passed to the consumer. + bool PrevDiagSilenced; + +public: + typedef Rewriter::buffer_iterator iterator; + + /// \brief Initialize a new fix-it rewriter. + FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr, + const LangOptions &LangOpts, FixItOptions *FixItOpts); + + /// \brief Destroy the fix-it rewriter. + ~FixItRewriter(); + + /// \brief Check whether there are modifications for a given file. + bool IsModified(FileID ID) const { + return Rewrite.getRewriteBufferFor(ID) != NULL; + } + + // Iteration over files with changes. + iterator buffer_begin() { return Rewrite.buffer_begin(); } + iterator buffer_end() { return Rewrite.buffer_end(); } + + /// \brief Write a single modified source file. + /// + /// \returns true if there was an error, false otherwise. + bool WriteFixedFile(FileID ID, raw_ostream &OS); + + /// \brief Write the modified source files. + /// + /// \returns true if there was an error, false otherwise. + bool WriteFixedFiles( + std::vector<std::pair<std::string, std::string> > *RewrittenFiles = 0); + + /// IncludeInDiagnosticCounts - This method (whose default implementation + /// returns true) indicates whether the diagnostics handled by this + /// DiagnosticConsumer should be included in the number of diagnostics + /// reported by DiagnosticsEngine. + virtual bool IncludeInDiagnosticCounts() const; + + /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or + /// capturing it to a log as needed. + virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const Diagnostic &Info); + + /// \brief Emit a diagnostic via the adapted diagnostic client. + void Diag(SourceLocation Loc, unsigned DiagID); + + DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const; +}; + +} + +#endif // LLVM_CLANG_REWRITE_FIX_IT_REWRITER_H diff --git a/include/clang/Rewrite/Frontend/FrontendActions.h b/include/clang/Rewrite/Frontend/FrontendActions.h new file mode 100644 index 0000000..ea876d9 --- /dev/null +++ b/include/clang/Rewrite/Frontend/FrontendActions.h @@ -0,0 +1,83 @@ +//===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_REWRITE_FRONTENDACTIONS_H +#define LLVM_CLANG_REWRITE_FRONTENDACTIONS_H + +#include "clang/Frontend/FrontendAction.h" + +namespace clang { +class FixItRewriter; +class FixItOptions; + +//===----------------------------------------------------------------------===// +// AST Consumer Actions +//===----------------------------------------------------------------------===// + +class HTMLPrintAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + StringRef InFile); +}; + +class FixItAction : public ASTFrontendAction { +protected: + OwningPtr<FixItRewriter> Rewriter; + OwningPtr<FixItOptions> FixItOpts; + + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + StringRef InFile); + + virtual bool BeginSourceFileAction(CompilerInstance &CI, + StringRef Filename); + + virtual void EndSourceFileAction(); + + virtual bool hasASTFileSupport() const { return false; } + +public: + FixItAction(); + ~FixItAction(); +}; + +/// \brief Emits changes to temporary files and uses them for the original +/// frontend action. +class FixItRecompile : public WrapperFrontendAction { +public: + FixItRecompile(FrontendAction *WrappedAction) + : WrapperFrontendAction(WrappedAction) {} + +protected: + virtual bool BeginInvocation(CompilerInstance &CI); +}; + +class RewriteObjCAction : public ASTFrontendAction { +protected: + virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, + StringRef InFile); +}; + +class RewriteMacrosAction : public PreprocessorFrontendAction { +protected: + void ExecuteAction(); +}; + +class RewriteTestAction : public PreprocessorFrontendAction { +protected: + void ExecuteAction(); +}; + +class RewriteIncludesAction : public PreprocessorFrontendAction { +protected: + void ExecuteAction(); +}; + +} // end namespace clang + +#endif diff --git a/include/clang/Rewrite/Frontend/Rewriters.h b/include/clang/Rewrite/Frontend/Rewriters.h new file mode 100644 index 0000000..f5ade5a --- /dev/null +++ b/include/clang/Rewrite/Frontend/Rewriters.h @@ -0,0 +1,35 @@ +//===--- Rewriters.h - Rewriter implementations -------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header contains miscellaneous utilities for various front-end actions. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_REWRITE_REWRITERS_H +#define LLVM_CLANG_REWRITE_REWRITERS_H + +#include "clang/Basic/LLVM.h" + +namespace clang { +class Preprocessor; +class PreprocessorOutputOptions; + +/// RewriteMacrosInInput - Implement -rewrite-macros mode. +void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS); + +/// DoRewriteTest - A simple test for the TokenRewriter class. +void DoRewriteTest(Preprocessor &PP, raw_ostream *OS); + +/// RewriteIncludesInInput - Implement -frewrite-includes mode. +void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, + const PreprocessorOutputOptions &Opts); + +} // end namespace clang + +#endif |