//===--- Format.h - Format C++ code -----------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// Various functions to configurably format source code. /// //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_FORMAT_FORMAT_H #define LLVM_CLANG_FORMAT_FORMAT_H #include "clang/Basic/LangOptions.h" #include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Inclusions/IncludeStyle.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/Regex.h" #include "llvm/Support/SourceMgr.h" #include <optional> #include <system_error> namespace llvm { namespace vfs { class FileSystem; } } // namespace llvm namespace clang { namespace format { enum class ParseError { … }; class ParseErrorCategory final : public std::error_category { … }; const std::error_category &getParseCategory(); std::error_code make_error_code(ParseError e); /// The ``FormatStyle`` is used to configure the formatting to follow /// specific guidelines. struct FormatStyle { … }; /// Returns a format style complying with the LLVM coding standards: /// http://llvm.org/docs/CodingStandards.html. FormatStyle getLLVMStyle( FormatStyle::LanguageKind Language = FormatStyle::LanguageKind::LK_Cpp); /// Returns a format style complying with one of Google's style guides: /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. /// http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml. /// https://developers.google.com/protocol-buffers/docs/style. FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language); /// Returns a format style complying with Chromium's style guide: /// http://www.chromium.org/developers/coding-style. FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language); /// Returns a format style complying with Mozilla's style guide: /// https://firefox-source-docs.mozilla.org/code-quality/coding-style/index.html. FormatStyle getMozillaStyle(); /// Returns a format style complying with Webkit's style guide: /// http://www.webkit.org/coding/coding-style.html FormatStyle getWebKitStyle(); /// Returns a format style complying with GNU Coding Standards: /// http://www.gnu.org/prep/standards/standards.html FormatStyle getGNUStyle(); /// Returns a format style complying with Microsoft style guide: /// https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017 FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language); FormatStyle getClangFormatStyle(); /// Returns style indicating formatting should be not applied at all. FormatStyle getNoStyle(); /// Gets a predefined style for the specified language by name. /// /// Currently supported names: LLVM, Google, Chromium, Mozilla. Names are /// compared case-insensitively. /// /// Returns ``true`` if the Style has been set. bool getPredefinedStyle(StringRef Name, FormatStyle::LanguageKind Language, FormatStyle *Style); /// Parse configuration from YAML-formatted text. /// /// Style->Language is used to get the base style, if the ``BasedOnStyle`` /// option is present. /// /// The FormatStyleSet of Style is reset. /// /// When ``BasedOnStyle`` is not present, options not present in the YAML /// document, are retained in \p Style. /// /// If AllowUnknownOptions is true, no errors are emitted if unknown /// format options are occurred. /// /// If set all diagnostics are emitted through the DiagHandler. std::error_code parseConfiguration(llvm::MemoryBufferRef Config, FormatStyle *Style, bool AllowUnknownOptions = false, llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr, void *DiagHandlerCtx = nullptr); /// Like above but accepts an unnamed buffer. inline std::error_code parseConfiguration(StringRef Config, FormatStyle *Style, bool AllowUnknownOptions = false) { … } /// Gets configuration in a YAML string. std::string configurationAsText(const FormatStyle &Style); /// Returns the replacements necessary to sort all ``#include`` blocks /// that are affected by ``Ranges``. tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName, unsigned *Cursor = nullptr); /// Returns the replacements corresponding to applying and formatting /// \p Replaces on success; otheriwse, return an llvm::Error carrying /// llvm::StringError. Expected<tooling::Replacements> formatReplacements(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style); /// Returns the replacements corresponding to applying \p Replaces and /// cleaning up the code after that on success; otherwise, return an llvm::Error /// carrying llvm::StringError. /// This also supports inserting/deleting C++ #include directives: /// - If a replacement has offset UINT_MAX, length 0, and a replacement text /// that is an #include directive, this will insert the #include into the /// correct block in the \p Code. /// - If a replacement has offset UINT_MAX, length 1, and a replacement text /// that is the name of the header to be removed, the header will be removed /// from \p Code if it exists. /// The include manipulation is done via ``tooling::HeaderInclude``, see its /// documentation for more details on how include insertion points are found and /// what edits are produced. Expected<tooling::Replacements> cleanupAroundReplacements(StringRef Code, const tooling::Replacements &Replaces, const FormatStyle &Style); /// Represents the status of a formatting attempt. struct FormattingAttemptStatus { … }; /// Reformats the given \p Ranges in \p Code. /// /// Each range is extended on either end to its next bigger logic unit, i.e. /// everything that might influence its formatting or might be influenced by its /// formatting. /// /// Returns the ``Replacements`` necessary to make all \p Ranges comply with /// \p Style. /// /// If ``Status`` is non-null, its value will be populated with the status of /// this formatting attempt. See \c FormattingAttemptStatus. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>", FormattingAttemptStatus *Status = nullptr); /// Same as above, except if ``IncompleteFormat`` is non-null, its value /// will be set to true if any of the affected ranges were not formatted due to /// a non-recoverable syntax error. tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName, bool *IncompleteFormat); /// Clean up any erroneous/redundant code in the given \p Ranges in \p /// Code. /// /// Returns the ``Replacements`` that clean up all \p Ranges in \p Code. tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); /// Fix namespace end comments in the given \p Ranges in \p Code. /// /// Returns the ``Replacements`` that fix the namespace comments in all /// \p Ranges in \p Code. tooling::Replacements fixNamespaceEndComments(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); /// Inserts or removes empty lines separating definition blocks including /// classes, structs, functions, namespaces, and enums in the given \p Ranges in /// \p Code. /// /// Returns the ``Replacements`` that inserts or removes empty lines separating /// definition blocks in all \p Ranges in \p Code. tooling::Replacements separateDefinitionBlocks(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); /// Sort consecutive using declarations in the given \p Ranges in /// \p Code. /// /// Returns the ``Replacements`` that sort the using declarations in all /// \p Ranges in \p Code. tooling::Replacements sortUsingDeclarations(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, StringRef FileName = "<stdin>"); /// Returns the ``LangOpts`` that the formatter expects you to set. /// /// \param Style determines specific settings for lexing mode. LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle()); /// Description to be used for help text for a ``llvm::cl`` option for /// specifying format style. The description is closely related to the operation /// of ``getStyle()``. extern const char *StyleOptionHelpDescription; /// The suggested format style to use by default. This allows tools using /// ``getStyle`` to have a consistent default style. /// Different builds can modify the value to the preferred styles. extern const char *DefaultFormatStyle; /// The suggested predefined style to use as the fallback style in ``getStyle``. /// Different builds can modify the value to the preferred styles. extern const char *DefaultFallbackStyle; /// Construct a FormatStyle based on ``StyleName``. /// /// ``StyleName`` can take several forms: /// * "{<key>: <value>, ...}" - Set specic style parameters. /// * "<style name>" - One of the style names supported by /// getPredefinedStyle(). /// * "file" - Load style configuration from a file called ``.clang-format`` /// located in one of the parent directories of ``FileName`` or the current /// directory if ``FileName`` is empty. /// * "file:<format_file_path>" to explicitly specify the configuration file to /// use. /// /// \param[in] StyleName Style name to interpret according to the description /// above. /// \param[in] FileName Path to start search for .clang-format if ``StyleName`` /// == "file". /// \param[in] FallbackStyle The name of a predefined style used to fallback to /// in case \p StyleName is "file" and no file can be found. /// \param[in] Code The actual code to be formatted. Used to determine the /// language if the filename isn't sufficient. /// \param[in] FS The underlying file system, in which the file resides. By /// default, the file system is the real file system. /// \param[in] AllowUnknownOptions If true, unknown format options only /// emit a warning. If false, errors are emitted on unknown format /// options. /// /// \returns FormatStyle as specified by ``StyleName``. If ``StyleName`` is /// "file" and no file is found, returns ``FallbackStyle``. If no style could be /// determined, returns an Error. Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyle, StringRef Code = "", llvm::vfs::FileSystem *FS = nullptr, bool AllowUnknownOptions = false, llvm::SourceMgr::DiagHandlerTy DiagHandler = nullptr); // Guesses the language from the ``FileName`` and ``Code`` to be formatted. // Defaults to FormatStyle::LK_Cpp. FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code); // Returns a string representation of ``Language``. inline StringRef getLanguageName(FormatStyle::LanguageKind Language) { … } bool isClangFormatOn(StringRef Comment); bool isClangFormatOff(StringRef Comment); } // end namespace format } // end namespace clang template <> struct std::is_error_code_enum<clang::format::ParseError> : std::true_type { … }; #endif // LLVM_CLANG_FORMAT_FORMAT_H