r191666 - Moving style option formatting to libFormat

David Blaikie dblaikie at gmail.com
Tue Oct 1 09:58:35 PDT 2013


Many of us compile(self hosting - were mostly interested in clang's
behavior here) with -Werror to ensure we keep the build warning free. This
also means we're pretty immediately impacted in the form of build breakage
whenever someone makes the build warning-unclean.
On Oct 1, 2013 6:14 AM, "Vane, Edwin" <edwin.vane at intel.com> wrote:

>  Sorry about that. I didn’t even see the warning or the file static
> variable.****
>
> ** **
>
> *From:* David Blaikie [mailto:dblaikie at gmail.com]
> *Sent:* Monday, September 30, 2013 1:37 PM
> *To:* Vane, Edwin
> *Cc:* llvm cfe
> *Subject:* Re: r191666 - Moving style option formatting to libFormat****
>
> ** **
>
> ** **
>
> ** **
>
> On Mon, Sep 30, 2013 at 6:31 AM, Edwin Vane <edwin.vane at intel.com> wrote:*
> ***
>
> Author: revane
> Date: Mon Sep 30 08:31:48 2013
> New Revision: 191666
>
> URL: http://llvm.org/viewvc/llvm-project?rev=191666&view=rev
> Log:
> Moving style option formatting to libFormat
>
> The help text for clang-format's -style option and the function that
> processes
> its value is moved to libFormat in this patch. The goal is to enable other
> tools that use libFormat and also have a -style option to behave
> consistently
> with clang-format.
>
>
> Modified:
>     cfe/trunk/include/clang/Format/Format.h
>     cfe/trunk/lib/Format/Format.cpp
>     cfe/trunk/tools/clang-format/ClangFormat.cpp
>
> Modified: cfe/trunk/include/clang/Format/Format.h
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=191666&r1=191665&r2=191666&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Format/Format.h (original)
> +++ cfe/trunk/include/clang/Format/Format.h Mon Sep 30 08:31:48 2013
> @@ -347,6 +347,30 @@ tooling::Replacements reformat(const For
>  LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard =
>                                        FormatStyle::LS_Cpp11);
>
> +/// \brief 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;
> +
> +/// \brief Construct a FormatStyle based on \c StyleName.
> +///
> +/// \c StyleName can take several forms:
> +/// \li "{<key>: <value>, ...}" - Set specic style parameters.
> +/// \li "<style name>" - One of the style names supported by
> +/// getPredefinedStyle().
> +/// \li "file" - Load style configuration from a file called
> '.clang-format'
> +/// located in one of the parent directories of \c FileName or the current
> +/// directory if \c FileName is empty.
> +///
> +/// \param[in] StyleName Style name to interpret according to the
> description
> +/// above.
> +/// \param[in] FileName Path to start search for .clang-format if \c
> StyleName
> +/// == "file".
> +///
> +/// \returns FormatStyle as specified by \c StyleName. If no style could
> be
> +/// determined, the default is LLVM Style (see getLLVMStyle()).
> +FormatStyle getStyle(StringRef StyleName, StringRef FileName);
> +
>  } // end namespace format
>  } // end namespace clang
>
>
> Modified: cfe/trunk/lib/Format/Format.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=191666&r1=191665&r2=191666&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Format/Format.cpp (original)
> +++ cfe/trunk/lib/Format/Format.cpp Mon Sep 30 08:31:48 2013
> @@ -27,6 +27,7 @@
>  #include "llvm/Support/Allocator.h"
>  #include "llvm/Support/Debug.h"
>  #include "llvm/Support/YAMLTraits.h"
> +#include "llvm/Support/Path.h"
>  #include <queue>
>  #include <string>
>
> @@ -1305,5 +1306,82 @@ LangOptions getFormattingLangOpts(Format
>    return LangOpts;
>  }
>
> +const char *StyleOptionHelpDescription =
> +    "Coding style, currently supports:\n"
> +    "  LLVM, Google, Chromium, Mozilla, WebKit.\n"
> +    "Use -style=file to load style configuration from\n"
> +    ".clang-format file located in one of the parent\n"
> +    "directories of the source file (or current\n"
> +    "directory for stdin).\n"
> +    "Use -style=\"{key: value, ...}\" to set specific\n"
> +    "parameters, e.g.:\n"
> +    "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
> +
> +FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
> +  // Fallback style in case the rest of this function can't determine a
> style.
> +  StringRef FallbackStyle = "LLVM";****
>
>  ** **
>
> Introducing this variable caused the file-scoped static FallbackStyle to
> become unused. I removed that variable in r191682 to fix the Clang -Werror
> build. Please adjust as necessary if my fix wasn't appropriate.
>
> - David****
>
>  ****
>
> +  FormatStyle Style;
> +  getPredefinedStyle(FallbackStyle, &Style);
> +
> +  if (StyleName.startswith("{")) {
> +    // Parse YAML/JSON style from the command line.
> +    if (llvm::error_code ec = parseConfiguration(StyleName, &Style)) {
> +      llvm::errs() << "Error parsing -style: " << ec.message()
> +                   << ", using " << FallbackStyle << " style\n";
> +    }
> +    return Style;
> +  }
> +
> +  if (!StyleName.equals_lower("file")) {
> +    if (!getPredefinedStyle(StyleName, &Style))
> +      llvm::errs() << "Invalid value for -style, using " << FallbackStyle
> +                   << " style\n";
> +    return Style;
> +  }
> +
> +  SmallString<128> Path(FileName);
> +  llvm::sys::fs::make_absolute(Path);
> +  for (StringRef Directory = Path;
> +       !Directory.empty();
> +       Directory = llvm::sys::path::parent_path(Directory)) {
> +    if (!llvm::sys::fs::is_directory(Directory))
> +      continue;
> +    SmallString<128> ConfigFile(Directory);
> +
> +    llvm::sys::path::append(ConfigFile, ".clang-format");
> +    DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
> +    bool IsFile = false;
> +    // Ignore errors from is_regular_file: we only need to know if we can
> read
> +    // the file or not.
> +    llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
> +
> +    if (!IsFile) {
> +      // Try _clang-format too, since dotfiles are not commonly used on
> Windows.
> +      ConfigFile = Directory;
> +      llvm::sys::path::append(ConfigFile, "_clang-format");
> +      DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
> +      llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
> +    }
> +
> +    if (IsFile) {
> +      OwningPtr<llvm::MemoryBuffer> Text;
> +      if (llvm::error_code ec = llvm::MemoryBuffer::getFile(ConfigFile,
> Text)) {
> +        llvm::errs() << ec.message() << "\n";
> +        continue;
> +      }
> +      if (llvm::error_code ec = parseConfiguration(Text->getBuffer(),
> &Style)) {
> +        llvm::errs() << "Error reading " << ConfigFile << ": " <<
> ec.message()
> +                     << "\n";
> +        continue;
> +      }
> +      DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile <<
> "\n");
> +      return Style;
> +    }
> +  }
> +  llvm::errs() << "Can't find usable .clang-format, using " <<
> FallbackStyle
> +               << " style\n";
> +  return Style;
> +}
> +
>  } // namespace format
>  } // namespace clang
>
> Modified: cfe/trunk/tools/clang-format/ClangFormat.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ClangFormat.cpp?rev=191666&r1=191665&r2=191666&view=diff
>
> ==============================================================================
> --- cfe/trunk/tools/clang-format/ClangFormat.cpp (original)
> +++ cfe/trunk/tools/clang-format/ClangFormat.cpp Mon Sep 30 08:31:48 2013
> @@ -63,15 +63,7 @@ LineRanges("lines", cl::desc("<start lin
>             cl::cat(ClangFormatCategory));
>  static cl::opt<std::string>
>      Style("style",
> -          cl::desc("Coding style, currently supports:\n"
> -                   "  LLVM, Google, Chromium, Mozilla, WebKit.\n"
> -                   "Use -style=file to load style configuration from\n"
> -                   ".clang-format file located in one of the parent\n"
> -                   "directories of the source file (or current\n"
> -                   "directory for stdin).\n"
> -                   "Use -style=\"{key: value, ...}\" to set specific\n"
> -                   "parameters, e.g.:\n"
> -                   "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""),
> +          cl::desc(clang::format::StyleOptionHelpDescription),
>            cl::init("file"), cl::cat(ClangFormatCategory));
>
>  static cl::opt<std::string>
> @@ -114,72 +106,6 @@ static FileID createInMemoryFile(StringR
>    return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);
>  }
>
> -FormatStyle getStyle(StringRef StyleName, StringRef FileName) {
> -  FormatStyle Style;
> -  getPredefinedStyle(FallbackStyle, &Style);
> -
> -  if (StyleName.startswith("{")) {
> -    // Parse YAML/JSON style from the command line.
> -    if (error_code ec = parseConfiguration(StyleName, &Style)) {
> -      llvm::errs() << "Error parsing -style: " << ec.message()
> -                   << ", using " << FallbackStyle << " style\n";
> -    }
> -    return Style;
> -  }
> -
> -  if (!StyleName.equals_lower("file")) {
> -    if (!getPredefinedStyle(StyleName, &Style))
> -      llvm::errs() << "Invalid value for -style, using " << FallbackStyle
> -                   << " style\n";
> -    return Style;
> -  }
> -
> -  if (FileName == "-")
> -    FileName = AssumeFilename;
> -  SmallString<128> Path(FileName);
> -  llvm::sys::fs::make_absolute(Path);
> -  for (StringRef Directory = Path;
> -       !Directory.empty();
> -       Directory = llvm::sys::path::parent_path(Directory)) {
> -    if (!llvm::sys::fs::is_directory(Directory))
> -      continue;
> -    SmallString<128> ConfigFile(Directory);
> -
> -    llvm::sys::path::append(ConfigFile, ".clang-format");
> -    DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
> -    bool IsFile = false;
> -    // Ignore errors from is_regular_file: we only need to know if we can
> read
> -    // the file or not.
> -    llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
> -
> -    if (!IsFile) {
> -      // Try _clang-format too, since dotfiles are not commonly used on
> Windows.
> -      ConfigFile = Directory;
> -      llvm::sys::path::append(ConfigFile, "_clang-format");
> -      DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
> -      llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);
> -    }
> -
> -    if (IsFile) {
> -      OwningPtr<MemoryBuffer> Text;
> -      if (error_code ec = MemoryBuffer::getFile(ConfigFile, Text)) {
> -        llvm::errs() << ec.message() << "\n";
> -        continue;
> -      }
> -      if (error_code ec = parseConfiguration(Text->getBuffer(), &Style)) {
> -        llvm::errs() << "Error reading " << ConfigFile << ": " <<
> ec.message()
> -                     << "\n";
> -        continue;
> -      }
> -      DEBUG(llvm::dbgs() << "Using configuration file " << ConfigFile <<
> "\n");
> -      return Style;
> -    }
> -  }
> -  llvm::errs() << "Can't find usable .clang-format, using " <<
> FallbackStyle
> -               << " style\n";
> -  return Style;
> -}
> -
>  // Parses <start line>:<end line> input to a pair of line numbers.
>  // Returns true on error.
>  static bool parseLineRange(StringRef Input, unsigned &FromLine,
> @@ -269,7 +195,8 @@ static bool format(std::string FileName)
>    if (fillRanges(Sources, ID, Code.get(), Ranges))
>      return true;
>
> -  FormatStyle FormatStyle = getStyle(Style, FileName);
> +  FormatStyle FormatStyle =
> +      getStyle(Style, (FileName == "-") ? AssumeFilename : FileName);
>    Lexer Lex(ID, Sources.getBuffer(ID), Sources,
>              getFormattingLangOpts(FormatStyle.Standard));
>    tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources,
> Ranges);
> @@ -340,8 +267,9 @@ int main(int argc, const char **argv) {
>      cl::PrintHelpMessage();
>
>    if (DumpConfig) {
> -    std::string Config = clang::format::configurationAsText(
> -        clang::format::getStyle(Style, FileNames.empty() ? "-" :
> FileNames[0]));
> +    std::string Config =
> +        clang::format::configurationAsText(clang::format::getStyle(
> +            Style, FileNames.empty() ? AssumeFilename : FileNames[0]));
>      llvm::outs() << Config << "\n";
>      return 0;
>    }
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits****
>
>  ** **
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131001/aa715fed/attachment.html>


More information about the cfe-commits mailing list