From bb235c67bc3e99d34c48c776c013f16c293f6000 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 4 Sep 2014 18:23:42 +0000 Subject: [PATCH] clang-format: [JS] Support alternative operator names as identifiers. Before: not. and . or . not_eq = 1; After: not.and.or.not_eq = 1; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217179 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 6 ++---- lib/Format/Format.cpp | 15 ++++++++------- tools/clang-format/ClangFormat.cpp | 2 +- unittests/Format/FormatTestJS.cpp | 4 ++++ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 8ee657ae07..8665b79199 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -506,10 +506,8 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, /// \brief Returns the \c LangOpts that the formatter expects you to set. /// -/// \param Standard determines lexing mode: LC_Cpp11 and LS_Auto turn on C++11 -/// lexing mode, LS_Cpp03 - C++03 mode. -LangOptions getFormattingLangOpts( - FormatStyle::LanguageStandard Standard = FormatStyle::LS_Cpp11); +/// \param Style determines specific settings for lexing mode. +LangOptions getFormattingLangOpts(const FormatStyle &Style = getLLVMStyle()); /// \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 diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a623ae1c71..c7bcc670ac 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1277,8 +1277,8 @@ public: encoding::Encoding Encoding) : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false), Column(0), TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), - Style(Style), IdentTable(getFormattingLangOpts()), Encoding(Encoding), - FirstInLineIndex(0), FormattingDisabled(false) { + Style(Style), IdentTable(getFormattingLangOpts(Style)), + Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false) { Lex.SetKeepWhitespaceMode(true); for (const std::string &ForEachMacro : Style.ForEachMacros) @@ -2002,7 +2002,7 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, FileID ID = SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, - getFormattingLangOpts(Style.Standard)); + getFormattingLangOpts(Style)); SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); std::vector CharRanges; for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { @@ -2013,13 +2013,14 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, return reformat(Style, Lex, SourceMgr, CharRanges); } -LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { +LangOptions getFormattingLangOpts(const FormatStyle &Style) { LangOptions LangOpts; LangOpts.CPlusPlus = 1; - LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; - LangOpts.CPlusPlus14 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; + LangOpts.CPlusPlus11 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; + LangOpts.CPlusPlus14 = Style.Standard == FormatStyle::LS_Cpp03 ? 0 : 1; LangOpts.LineComment = 1; - LangOpts.CXXOperatorNames = 1; + LangOpts.CXXOperatorNames = + Style.Language != FormatStyle::LK_JavaScript ? 1 : 0; LangOpts.Bool = 1; LangOpts.ObjC1 = 1; LangOpts.ObjC2 = 1; diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index cebb2757d4..7dda9c6e13 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -226,7 +226,7 @@ static bool format(StringRef FileName) { FormatStyle FormatStyle = getStyle( Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle); Lexer Lex(ID, Sources.getBuffer(ID), Sources, - getFormattingLangOpts(FormatStyle.Standard)); + getFormattingLangOpts(FormatStyle)); tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges); if (OutputXML) { llvm::outs() diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 5bb1a3850c..fcddba72a8 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -83,6 +83,10 @@ TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { verifyFormat("var b = a.map((x) => x + 1);"); } +TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { + verifyFormat("not.and.or.not_eq = 1;"); +} + TEST_F(FormatTestJS, ES6DestructuringAssignment) { verifyFormat("var [a, b, c] = [1, 2, 3];"); verifyFormat("var {a, b} = {a: 1, b: 2};"); -- 2.40.0