]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support alternative operator names as identifiers.
authorDaniel Jasper <djasper@google.com>
Thu, 4 Sep 2014 18:23:42 +0000 (18:23 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 4 Sep 2014 18:23:42 +0000 (18:23 +0000)
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
lib/Format/Format.cpp
tools/clang-format/ClangFormat.cpp
unittests/Format/FormatTestJS.cpp

index 8ee657ae07a3413dfc23cce783f116fa77de7284..8665b791998d80ab9997759cdc556700ffe8f2ad 100644 (file)
@@ -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
index a623ae1c7167b65ac3e6d27f9e1b76eb2fb31cac..c7bcc670ac7b1d83045d35a8f170e65c38b2a2e3 100644 (file)
@@ -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<CharSourceRange> 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;
index cebb2757d4da0ae953ef6f81f0dd3cbd810320c9..7dda9c6e1387de9ca4c08cac25df0b1c5445452d 100644 (file)
@@ -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()
index 5bb1a3850c8e9c89e8f0982726652a6d7499b1e1..fcddba72a8b1a942fb82b5eea5c62d2491b7d04f 100644 (file)
@@ -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};");