[clang] 7ad60f6 - [clang-format] fix BreakBeforeBraces.MultiLine with for each macros

2020-08-05 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-08-05T14:31:42-04:00
New Revision: 7ad60f6452ff483912776ef6eda4e28c60d9da6e

URL: 
https://github.com/llvm/llvm-project/commit/7ad60f6452ff483912776ef6eda4e28c60d9da6e
DIFF: 
https://github.com/llvm/llvm-project/commit/7ad60f6452ff483912776ef6eda4e28c60d9da6e.diff

LOG: [clang-format] fix BreakBeforeBraces.MultiLine with for each macros
Summary:
The MultiLine option in BreakBeforeBraces was only handling standard
control statement, leading to invalid indentation with for each macros:

Previous behavior:

/* invalid: brace should be on the same line */
Q_FOREACH(int a; list)
{
foo();
}

/* valid */
Q_FOREACH(int longVariable;
  list)
{
foo();
}

To fix this, simply add the TT_ForEachMacro kind in the list of
recognized control statements for the multiline option.

This is a fix for https://bugs.llvm.org/show_bug.cgi?id=44632

Reviewers: MyDeveloperDay, mitchell-stellar

Reviewed by: mitchell-stellar

Contributed by: vthib

Subscribers: cfe-commits

Tags: #clang, #clang-format, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D85304

Added: 


Modified: 
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index ec0c628fe750..d8f718306a45 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -309,7 +309,8 @@ class LineJoiner {
 // Try to merge a control statement block with left brace wrapped
 if (I[1]->First->is(tok::l_brace) &&
 (TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do) ||
+ tok::kw_switch, tok::kw_try, tok::kw_do,
+ TT_ForEachMacro) ||
  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
   TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
 Style.BraceWrapping.AfterControlStatement ==

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 83dd5bbba911..d8642062fb6f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1663,6 +1663,20 @@ TEST_F(FormatTest, MultiLineControlStatements) {
 "  foo();\n"
 "}",
 format("for(int i=0;i<10;++i){foo();}", Style));
+  EXPECT_EQ("foreach (int i,\n"
+" list)\n"
+"{\n"
+"  foo();\n"
+"}",
+format("foreach(int i, list){foo();}", Style));
+  Style.ColumnLimit =
+  40; // to concentrate at brace wrapping, not line wrap due to column 
limit
+  EXPECT_EQ("foreach (int i, list) {\n"
+"  foo();\n"
+"}",
+format("foreach(int i, list){foo();}", Style));
+  Style.ColumnLimit =
+  20; // to concentrate at brace wrapping, not line wrap due to column 
limit
   EXPECT_EQ("while (foo || bar ||\n"
 "   baz)\n"
 "{\n"



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] c5c487f - Revert "[clang-format] Add option to specify explicit config file"

2020-03-11 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-03-11T16:14:42-04:00
New Revision: c5c487f0d4c6720f4384f670490086e723f5fe32

URL: 
https://github.com/llvm/llvm-project/commit/c5c487f0d4c6720f4384f670490086e723f5fe32
DIFF: 
https://github.com/llvm/llvm-project/commit/c5c487f0d4c6720f4384f670490086e723f5fe32.diff

LOG: Revert "[clang-format] Add option to specify explicit config file"
There were a number of unexpected test failures.

This reverts commit 10b1a87ba35d386b718f0e83c1d750631705b220.

Added: 


Modified: 
clang/docs/ClangFormat.rst
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 633cffa55537..1138b2332670 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -75,10 +75,6 @@ to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
  .clang-format file located in one of the 
parent
  directories of the source file (or current
  directory for stdin).
- Use -style=file: to load 
style
- configuration from a format file located at
- . This path can be absolute 
or
- relative to the working directory.
  Use -style="{key: value, ...}" to set specific
  parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 
8}"

diff  --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index 99a6a7df790b..50b4ff5d9010 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -24,10 +24,6 @@ try to find the ``.clang-format`` file located in the 
closest parent directory
 of the input file. When the standard input is used, the search is started from
 the current directory.
 
-When using ``-style=file:, :program:`clang-format` for 
-each input file will use the format file located at ``.
-The path may be absolute or relative to the working directory.
-
 The ``.clang-format`` file uses YAML format:
 
 .. code-block:: yaml

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ce15513c1c8c..664ae4e4167c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -253,10 +253,6 @@ clang-format
   bar();
 });
 
-- The command line argument `-style=` has been extended so that a 
specific
-  format file at location  can be selected. This is supported
-  via the syntax: `-style=file:`.
-
 libclang
 
 

diff  --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 8c1cb6ea3986..d4f76c87c14e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2476,7 +2476,6 @@ extern const char *DefaultFallbackStyle;
 /// * "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:" to explicitly specify the configuration file to use.
 ///
 /// \param[in] StyleName Style name to interpret according to the description
 /// above.

diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 4c20eb2941ba..031312bd16d8 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2640,8 +2640,6 @@ const char *StyleOptionHelpDescription =
 ".clang-format file located in one of the parent\n"
 "directories of the source file (or current\n"
 "directory for stdin).\n"
-"Use -style=file: to explicitly specify"
-"the configuration file.\n"
 "Use -style=\"{key: value, ...}\" to set specific\n"
 "parameters, e.g.:\n"
 "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
@@ -2687,29 +2685,6 @@ FormatStyle::LanguageKind guessLanguage(StringRef 
FileName, StringRef Code) {
   return GuessedLanguage;
 }
 
-/// Attempts to load a format file
-llvm::Expected LoadConfigFile(StringRef ConfigFile,
-   llvm::vfs::FileSystem *FS,
-   bool *IsSuitable) {
-  FormatStyle Style = getLLVMStyle();
-  *IsSuitable = true;
-
-  llvm::ErrorOr> Text =
-  FS->getBufferForFile(ConfigFile.str());
-  if (std::error_code EC = Text.getError())
-return make_string_error(EC.message());
-  std::error_code ParserErrorCode =
-  parseConfiguration(Text.get()->getBuffer(), &Style);
-  if (ParserErrorCode == ParseError::Unsuitable) {
-*IsSuitable = false;
-return Style;
-  } else if (ParserErrorCode != ParseError::Success

[clang] 10b1a87 - [clang-format] Add option to specify explicit config file

2020-03-11 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-03-11T15:56:44-04:00
New Revision: 10b1a87ba35d386b718f0e83c1d750631705b220

URL: 
https://github.com/llvm/llvm-project/commit/10b1a87ba35d386b718f0e83c1d750631705b220
DIFF: 
https://github.com/llvm/llvm-project/commit/10b1a87ba35d386b718f0e83c1d750631705b220.diff

LOG: [clang-format] Add option to specify explicit config file
Summary:
This diff extends the -style=file option to allow a config file to be specified 
explicitly. This is useful (for instance) when adding IDE commands to reformat 
code to a personal style.

Reviewers: djasper, ioeric, krasimir, MyDeveloperDay

Reviewed by: MyDeveloperDay

Contributed by: tnorth

Subscribers: cfe-commits, lebedev.ri, MyDeveloperDay, klimek, sammccall, 
mitchell-stellar

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D72326

Added: 


Modified: 
clang/docs/ClangFormat.rst
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 1138b2332670..633cffa55537 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -75,6 +75,10 @@ to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.
  .clang-format file located in one of the 
parent
  directories of the source file (or current
  directory for stdin).
+ Use -style=file: to load 
style
+ configuration from a format file located at
+ . This path can be absolute 
or
+ relative to the working directory.
  Use -style="{key: value, ...}" to set specific
  parameters, e.g.:
-style="{BasedOnStyle: llvm, IndentWidth: 
8}"

diff  --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index 50b4ff5d9010..99a6a7df790b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -24,6 +24,10 @@ try to find the ``.clang-format`` file located in the 
closest parent directory
 of the input file. When the standard input is used, the search is started from
 the current directory.
 
+When using ``-style=file:, :program:`clang-format` for 
+each input file will use the format file located at ``.
+The path may be absolute or relative to the working directory.
+
 The ``.clang-format`` file uses YAML format:
 
 .. code-block:: yaml

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 664ae4e4167c..ce15513c1c8c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -253,6 +253,10 @@ clang-format
   bar();
 });
 
+- The command line argument `-style=` has been extended so that a 
specific
+  format file at location  can be selected. This is supported
+  via the syntax: `-style=file:`.
+
 libclang
 
 

diff  --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index d4f76c87c14e..8c1cb6ea3986 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2476,6 +2476,7 @@ extern const char *DefaultFallbackStyle;
 /// * "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:" to explicitly specify the configuration file to use.
 ///
 /// \param[in] StyleName Style name to interpret according to the description
 /// above.

diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 031312bd16d8..4c20eb2941ba 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2640,6 +2640,8 @@ const char *StyleOptionHelpDescription =
 ".clang-format file located in one of the parent\n"
 "directories of the source file (or current\n"
 "directory for stdin).\n"
+"Use -style=file: to explicitly specify"
+"the configuration file.\n"
 "Use -style=\"{key: value, ...}\" to set specific\n"
 "parameters, e.g.:\n"
 "  -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
@@ -2685,6 +2687,29 @@ FormatStyle::LanguageKind guessLanguage(StringRef 
FileName, StringRef Code) {
   return GuessedLanguage;
 }
 
+/// Attempts to load a format file
+llvm::Expected LoadConfigFile(StringRef ConfigFile,
+   llvm::vfs::FileSystem *FS,
+   bool *IsSuitable) {
+  FormatStyle Style = getLLVMStyle();
+  *IsSuitable = true;
+
+  llvm::ErrorOr> Text =
+  FS->getBufferForF

[clang] 2eff1c3 - [clang-format] Extend AllowShortLoopsOnASingleLine to do ... while loops.

2020-03-06 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-03-06T11:13:23-05:00
New Revision: 2eff1c3ce48ef529064e2dc006d57c37da0b4d84

URL: 
https://github.com/llvm/llvm-project/commit/2eff1c3ce48ef529064e2dc006d57c37da0b4d84
DIFF: 
https://github.com/llvm/llvm-project/commit/2eff1c3ce48ef529064e2dc006d57c37da0b4d84.diff

LOG: [clang-format] Extend AllowShortLoopsOnASingleLine to do ... while loops.
Summary:
If AllowShortLoopsOnASingleLine is enabled,

  do
a++;
  while (true);

becomes

  do a++;
  while (true);

Reviewers: MyDeveloperDay, mitchell-stellar

Reviewed by: mitchell-stellar

Contributed by: DaanDeMeyer

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D75022

Added: 


Modified: 
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index da9ee41da71b..84ccbec2150d 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -411,7 +411,7 @@ class LineJoiner {
  ? tryMergeSimpleControlStatement(I, E, Limit)
  : 0;
 }
-if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
+if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do)) {
   return Style.AllowShortLoopsOnASingleLine
  ? tryMergeSimpleControlStatement(I, E, Limit)
  : 0;
@@ -514,7 +514,10 @@ class LineJoiner {
   return 0;
 Limit = limitConsideringMacros(I + 1, E, Limit);
 AnnotatedLine &Line = **I;
-if (Line.Last->isNot(tok::r_paren))
+if (!Line.First->is(tok::kw_do) && Line.Last->isNot(tok::r_paren))
+  return 0;
+// Only merge do while if do is the only statement on the line.
+if (Line.First->is(tok::kw_do) && !Line.Last->is(tok::kw_do))
   return 0;
 if (1 + I[1]->Last->TotalLength > Limit)
   return 0;

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index cb052ecaf111..a6f35ba27069 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -556,6 +556,30 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   verifyFormat("for (;;) /* still don't merge */\n"
"  continue;",
AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted 
diff erently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] 73d9361 - [clang-tidy] modernize-use-using uses AST and now supports struct defintions and multiple types in a typedef

2020-01-07 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-01-07T16:36:11-05:00
New Revision: 73d93617d3ae23bad232fa3a160c638728c71c01

URL: 
https://github.com/llvm/llvm-project/commit/73d93617d3ae23bad232fa3a160c638728c71c01
DIFF: 
https://github.com/llvm/llvm-project/commit/73d93617d3ae23bad232fa3a160c638728c71c01.diff

LOG: [clang-tidy] modernize-use-using uses AST and now supports struct 
defintions and multiple types in a typedef

Summary:
It now handles `typedef`s that include comma-separated multiple types, and 
handles embedded struct definitions, which previously could not be 
automatically converted.

For example, with this patch `modernize-use-using` now can convert:

typedef struct { int a; } R_t, *R_p;

to:

using R_t = struct { int a; };
using R_p = R_t*;

`-ast-dump` showed that the `CXXRecordDecl` definitions and multiple 
`TypedefDecl`s come consecutively in the tree, so `check()` stores information 
between calls to determine when it is receiving a second or additional 
`TypedefDecl` within a single `typedef`, or when the current `TypedefDecl` 
refers to an embedded `CXXRecordDecl` like a `struct`.

Reviewers: alexfh, aaron.ballman

Patch by: poelmanc

Subscribers: riccibruno, sammccall, cfe-commits, aaron.ballman

Tags: clang-tools-extra, clang

Differential Revision: https://reviews.llvm.org/D70270

Added: 


Modified: 
clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
clang-tools-extra/clang-tidy/modernize/UseUsingCheck.h
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/modernize-use-using.rst
clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
clang/include/clang/Basic/SourceLocation.h

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index c9ca8a6114a5..ce8d00f358ab 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -25,107 +25,88 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
 return;
   Finder->addMatcher(typedefDecl(unless(isInstantiated())).bind("typedef"),
  this);
+  // This matcher used to find structs defined in source code within typedefs.
+  // They appear in the AST just *prior* to the typedefs.
+  Finder->addMatcher(cxxRecordDecl(unless(isImplicit())).bind("struct"), this);
 }
 
-// Checks if 'typedef' keyword can be removed - we do it only if
-// it is the only declaration in a declaration chain.
-static bool CheckRemoval(SourceManager &SM, SourceLocation StartLoc,
- ASTContext &Context) {
-  assert(StartLoc.isFileID() && "StartLoc must not be in a macro");
-  std::pair LocInfo = SM.getDecomposedLoc(StartLoc);
-  StringRef File = SM.getBufferData(LocInfo.first);
-  const char *TokenBegin = File.data() + LocInfo.second;
-  Lexer DeclLexer(SM.getLocForStartOfFile(LocInfo.first), 
Context.getLangOpts(),
-  File.begin(), TokenBegin, File.end());
-
-  Token Tok;
-  int NestingLevel = 0; // Parens, braces, and square brackets
-  int AngleBracketLevel = 0;
-  bool FoundTypedef = false;
-
-  while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
-switch (Tok.getKind()) {
-case tok::l_brace:
-  if (NestingLevel == 0 && AngleBracketLevel == 0) {
-// At top level, this might be the `typedef struct {...} T;` case.
-// Inside parens, square brackets, or angle brackets it's not.
-return false;
-  }
-  ++NestingLevel;
-  break;
-case tok::l_paren:
-case tok::l_square:
-  ++NestingLevel;
-  break;
-case tok::r_brace:
-case tok::r_paren:
-case tok::r_square:
-  --NestingLevel;
-  break;
-case tok::less:
-  // If not nested in paren/brace/square bracket, treat as opening angle 
bracket.
-  if (NestingLevel == 0)
-++AngleBracketLevel;
-  break;
-case tok::greater:
-  // Per C++ 17 Draft N4659, Section 17.2/3
-  //   https://timsong-cpp.github.io/cppwp/n4659/temp.names#3:
-  // "When parsing a template-argument-list, the first non-nested > is
-  // taken as the ending delimiter rather than a greater-than operator."
-  // If not nested in paren/brace/square bracket, treat as closing angle 
bracket.
-  if (NestingLevel == 0)
---AngleBracketLevel;
-  break;
-case tok::comma:
-  if (NestingLevel == 0 && AngleBracketLevel == 0) {
-// If there is a non-nested comma we have two or more declarations in 
this chain.
-return false;
-  }
-  break;
-case tok::raw_identifier:
-  if (Tok.getRawIdentifier() == "typedef") {
-FoundTypedef = true;
-  }
-  break;
-default:
-  break;
-}
+void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
+  // Match CXXRecordDecl only to store

[clang] d45aafa - [clang-format] fix conflict between FormatStyle::BWACS_MultiLine and BeforeCatch/BeforeElse

2020-01-06 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2020-01-06T09:21:41-05:00
New Revision: d45aafa2fbcf66f3dafdc7c5e0a0ce3709914cbc

URL: 
https://github.com/llvm/llvm-project/commit/d45aafa2fbcf66f3dafdc7c5e0a0ce3709914cbc
DIFF: 
https://github.com/llvm/llvm-project/commit/d45aafa2fbcf66f3dafdc7c5e0a0ce3709914cbc.diff

LOG: [clang-format] fix conflict between FormatStyle::BWACS_MultiLine and 
BeforeCatch/BeforeElse

Summary:
Found a bug introduced with BraceWrappingFlags AfterControlStatement MultiLine. 
This feature conflicts with the existing BeforeCatch and BeforeElse flags.

For example, our team uses BeforeElse.

if (foo ||
bar) {
  doSomething();
}
else {
  doSomethingElse();
}

If we enable MultiLine (which we'd really love to do) we expect it to work like 
this:

if (foo ||
bar)
{
  doSomething();
}
else {
  doSomethingElse();
}

What we actually get is:

if (foo ||
bar)
{
  doSomething();
}
else
{
  doSomethingElse();
}

Reviewers: MyDeveloperDay, Bouska, mitchell-stellar

Patch by: pastey

Subscribers: Bouska, cfe-commits

Tags: clang

Differential Revision: https://reviews.llvm.org/D71939

Added: 


Modified: 
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 8b8d357d9cbe..fec85f1174da 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -326,6 +326,19 @@ class LineJoiner {
   FormatStyle::BWACS_Always)
  ? tryMergeSimpleBlock(I, E, Limit)
  : 0;
+} else if (I[1]->First->is(tok::l_brace) &&
+   TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
+   Style.BraceWrapping.AfterControlStatement ==
+   FormatStyle::BWACS_MultiLine) {
+  // This case if 
diff erent from the upper BWACS_MultiLine processing
+  // in that a preceding r_brace is not on the same line as else/catch
+  // most likely because of BeforeElse/BeforeCatch set to true.
+  // If the line length doesn't fit ColumnLimit, leave l_brace on the
+  // next line to respect the BWACS_MultiLine.
+  return (Style.ColumnLimit == 0 ||
+  TheLine->Last->TotalLength <= Style.ColumnLimit)
+ ? 1
+ : 0;
 }
 // Try to merge either empty or one-line block if is precedeed by control
 // statement token

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 069542683c0d..2d67b9759d7f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1565,6 +1565,41 @@ TEST_F(FormatTest, MultiLineControlStatements) {
 "  baz();\n"
 "}",
 format("try{foo();}catch(Exception&bar){baz();}", Style));
+  Style.ColumnLimit =
+  40; // to concentrate at brace wrapping, not line wrap due to column 
limit
+  EXPECT_EQ("try {\n"
+"  foo();\n"
+"} catch (Exception &bar) {\n"
+"  baz();\n"
+"}",
+format("try{foo();}catch(Exception&bar){baz();}", Style));
+  Style.ColumnLimit =
+  20; // to concentrate at brace wrapping, not line wrap due to column 
limit
+
+  Style.BraceWrapping.BeforeElse = true;
+  EXPECT_EQ(
+  "if (foo) {\n"
+  "  bar();\n"
+  "}\n"
+  "else if (baz ||\n"
+  " quux)\n"
+  "{\n"
+  "  foobar();\n"
+  "}\n"
+  "else {\n"
+  "  barbaz();\n"
+  "}",
+  format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
+ Style));
+
+  Style.BraceWrapping.BeforeCatch = true;
+  EXPECT_EQ("try {\n"
+"  foo();\n"
+"}\n"
+"catch (...) {\n"
+"  baz();\n"
+"}",
+format("try{foo();}catch(...){baz();}", Style));
 }
 
 
//===--===//
@@ -14881,7 +14916,7 @@ TEST_F(FormatTest, AmbersandInLamda) {
   verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle);
 }
 
- TEST_F(FormatTest, SpacesInConditionalStatement) {
+TEST_F(FormatTest, SpacesInConditionalStatement) {
   FormatStyle Spaces = getLLVMStyle();
   Spaces.SpacesInConditionalStatement = true;
   verifyFormat("for ( int i = 0; i; i++ )\n  continue;", Spaces);



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 26748a3 - [clang-format] Add new option to add spaces around conditions

2019-12-03 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-12-03T12:20:54-05:00
New Revision: 26748a321e20a7aa952ce8daa4f030c384ae7032

URL: 
https://github.com/llvm/llvm-project/commit/26748a321e20a7aa952ce8daa4f030c384ae7032
DIFF: 
https://github.com/llvm/llvm-project/commit/26748a321e20a7aa952ce8daa4f030c384ae7032.diff

LOG: [clang-format] Add new option to add spaces around conditions
Summary:
This diff adds a new option SpacesAroundConditions that inserts spaces inside 
the braces for conditional statements.

Reviewers: klimek, owenpan, mitchell-stellar, MyDeveloperDay

Patch by: timwoj

Subscribers: rsmmr, cfe-commits

Tags: clang, clang-format

Differential Revision: https://reviews.llvm.org/D68346

Added: 


Modified: 
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/docs/ClangFormatStyleOptions.rst 
b/clang/docs/ClangFormatStyleOptions.rst
index f438ec7f871b..2f7483435fd4 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2328,6 +2328,9 @@ the configuration (without a prefix: ``Auto``).
  true:  false:
  x = ( int32 )y vs. x = (int32)y
 
+**SpacesInConditionalStatement** (``bool``)
+  If ``true``, spaces will be inserted around if/for/while (and similar) 
conditions.
+
 **SpacesInContainerLiterals** (``bool``)
   If ``true``, spaces are inserted inside container literals (e.g.
   ObjC and Javascript array and dict literals).

diff  --git a/clang/include/clang/Format/Format.h 
b/clang/include/clang/Format/Format.h
index 604a935f3a28..f17a10c7f5c8 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1953,6 +1953,15 @@ struct FormatStyle {
   /// \endcode
   bool SpacesInAngles;
 
+  /// If ``true``, spaces will be inserted around if/for/switch/while
+  /// conditions.
+  /// \code
+  ///true:  false:
+  ///if ( a )  { ... }  vs. if (a) { ... }
+  ///while ( i < 5 )  { ... }   while (i < 5) { ... }
+  /// \endcode
+  bool SpacesInConditionalStatement;
+
   /// If ``true``, spaces are inserted inside container literals (e.g.
   /// ObjC and Javascript array and dict literals).
   /// \code{.js}
@@ -2155,6 +2164,7 @@ struct FormatStyle {
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
SpacesInAngles == R.SpacesInAngles &&
+   SpacesInConditionalStatement == R.SpacesInConditionalStatement &&
SpacesInContainerLiterals == R.SpacesInContainerLiterals &&
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpacesInParentheses == R.SpacesInParentheses &&

diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 4e42bab56182..f12bca48c630 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -537,6 +537,8 @@ template <> struct MappingTraits {
 IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
 IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
+IO.mapOptional("SpacesInConditionalStatement",
+   Style.SpacesInConditionalStatement);
 IO.mapOptional("SpacesInContainerLiterals",
Style.SpacesInContainerLiterals);
 IO.mapOptional("SpacesInCStyleCastParentheses",
@@ -817,6 +819,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind 
Language) {
   LLVMStyle.SpaceBeforeCpp11BracedList = false;
   LLVMStyle.SpaceBeforeSquareBrackets = false;
   LLVMStyle.SpacesInAngles = false;
+  LLVMStyle.SpacesInConditionalStatement = false;
 
   LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
   LLVMStyle.PenaltyBreakComment = 300;

diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 74ff5b531496..93cb36961ee5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2592,6 +2592,13 @@ bool TokenAnnotator::spaceRequiredBeforeParens(const 
FormatToken &Right) const {
   Right.ParameterCount > 0);
 }
 
+/// Returns \c true if the token is followed by a boolean condition, \c false
+/// otherwise.
+static bool isKeywordWithCondition(const FormatToken &Tok) {
+  return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
+ tok::kw_constexpr);
+};
+
 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
   const FormatToken &Left,
   const FormatToken &Right) {
@@ -2610,6 +2617,15 @@ bool TokenAnnotator::spaceRequiredBetween(const 

[clang-tools-extra] 24aafca - [clang-tidy] modernize-use-equals-default avoid adding redundant semicolons

2019-11-20 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-20T18:08:37-05:00
New Revision: 24aafcadff3851ec3a0c42303fec63e815b19566

URL: 
https://github.com/llvm/llvm-project/commit/24aafcadff3851ec3a0c42303fec63e815b19566
DIFF: 
https://github.com/llvm/llvm-project/commit/24aafcadff3851ec3a0c42303fec63e815b19566.diff

LOG: [clang-tidy] modernize-use-equals-default avoid adding redundant semicolons

Summary:
`modernize-use-equals-default` replaces default constructors/destructors with 
`= default;`. When the optional semicolon after a member function is present, 
this results in two consecutive semicolons.

This patch checks to see if the next non-comment token after the code to be 
replaced is a semicolon, and if so offers a replacement of `= default` rather 
than `= default;`.

This patch adds trailing comments and semicolons to about 5 existing tests.

Reviewers: malcolm.parsons, angelgarcia, aaron.ballman, alexfh

Patch by: poelmanc

Subscribers: MyDeveloperDay, JonasToth, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70144

Added: 


Modified: 
clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
clang-tools-extra/clang-tidy/utils/LexerUtils.h
clang-tools-extra/docs/ReleaseNotes.rst

clang-tools-extra/test/clang-tidy/checkers/modernize-use-equals-default-copy.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-use-equals-default.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
index 991eada514d3..0309fa8d0a37 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -10,6 +10,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/Lex/Lexer.h"
+#include "../utils/LexerUtils.h"
 
 using namespace clang::ast_matchers;
 
@@ -302,9 +303,16 @@ void UseEqualsDefaultCheck::check(const 
MatchFinder::MatchResult &Result) {
   auto Diag = diag(Location, "use '= default' to define a trivial " +
  SpecialFunctionName);
 
-  if (ApplyFix)
-Diag << FixItHint::CreateReplacement(Body->getSourceRange(), "= default;")
+  if (ApplyFix) {
+// Skipping comments, check for a semicolon after Body->getSourceRange()
+Optional Token = utils::lexer::findNextTokenSkippingComments(
+Body->getSourceRange().getEnd().getLocWithOffset(1),
+Result.Context->getSourceManager(), Result.Context->getLangOpts());
+StringRef Replacement =
+Token && Token->is(tok::semi) ? "= default" : "= default;";
+Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement)
  << RemoveInitializers;
+  }
 }
 
 } // namespace modernize

diff  --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp 
b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
index a6e361d66aa7..e80fda6e318e 100644
--- a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
+++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
@@ -68,6 +68,16 @@ SourceLocation findNextTerminator(SourceLocation Start, 
const SourceManager &SM,
   return findNextAnyTokenKind(Start, SM, LangOpts, tok::comma, tok::semi);
 }
 
+Optional findNextTokenSkippingComments(SourceLocation Start,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  Optional CurrentToken;
+  do {
+CurrentToken = Lexer::findNextToken(Start, SM, LangOpts);
+  } while (CurrentToken && CurrentToken->is(tok::comment));
+  return CurrentToken;
+}
+
 bool rangeContainsExpansionsOrDirectives(SourceRange Range,
  const SourceManager &SM,
  const LangOptions &LangOpts) {

diff  --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.h 
b/clang-tools-extra/clang-tidy/utils/LexerUtils.h
index 8330266e6341..2c4a2518259d 100644
--- a/clang-tools-extra/clang-tidy/utils/LexerUtils.h
+++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.h
@@ -80,6 +80,11 @@ SourceLocation findNextAnyTokenKind(SourceLocation Start,
   }
 }
 
+// Finds next token that's not a comment.
+Optional findNextTokenSkippingComments(SourceLocation Start,
+  const SourceManager &SM,
+  const LangOptions &LangOpts);
+
 /// Re-lex the provide \p Range and return \c false if either a macro spans
 /// multiple tokens, a pre-processor directive or failure to retrieve the
 /// next token is found, otherwise \c true.

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index fc61bda92541..09dc28ae1715 100644
--- a/clang-tools-extra/docs/Releas

[clang-tools-extra] 41ee54e - Revert "[clang-tidy] Fix readability-redundant-string-init for c++17/c++2a"

2019-11-19 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-19T07:52:31-05:00
New Revision: 41ee54e5d18858c56725485ef637ad5a686368f4

URL: 
https://github.com/llvm/llvm-project/commit/41ee54e5d18858c56725485ef637ad5a686368f4
DIFF: 
https://github.com/llvm/llvm-project/commit/41ee54e5d18858c56725485ef637ad5a686368f4.diff

LOG: Revert "[clang-tidy] Fix readability-redundant-string-init for c++17/c++2a"

This reverts commit 06f3dabe4a2e85a32ade27c0769b6084c828a206.

Added: 


Modified: 
clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/modernize-use-override.rst

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
index 8ee77ccd16ff..2f15213dca8c 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
@@ -20,13 +20,11 @@ namespace modernize {
 UseOverrideCheck::UseOverrideCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
   IgnoreDestructors(Options.get("IgnoreDestructors", false)),
-  AllowOverrideAndFinal(Options.get("AllowOverrideAndFinal", false)),
   OverrideSpelling(Options.get("OverrideSpelling", "override")),
   FinalSpelling(Options.get("FinalSpelling", "final")) {}
 
 void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IgnoreDestructors", IgnoreDestructors);
-  Options.store(Opts, "AllowOverrideAndFinal", AllowOverrideAndFinal);
   Options.store(Opts, "OverrideSpelling", OverrideSpelling);
   Options.store(Opts, "FinalSpelling", FinalSpelling);
 }
@@ -105,8 +103,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
   bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
   unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
 
-  if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
-  (!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
+  if (!OnlyVirtualSpecified && KeywordCount == 1)
 return; // Nothing to do.
 
   std::string Message;
@@ -116,9 +113,8 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
 Message = "annotate this function with '%0' or (rarely) '%1'";
   } else {
 StringRef Redundant =
-HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
-  ? "'virtual' and '%0' are"
-  : "'virtual' is")
+HasVirtual ? (HasOverride && HasFinal ? "'virtual' and '%0' are"
+  : "'virtual' is")
: "'%0' is";
 StringRef Correct = HasFinal ? "'%1'" : "'%0'";
 
@@ -215,7 +211,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
 Diag << FixItHint::CreateInsertion(InsertLoc, ReplacementText);
   }
 
-  if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
+  if (HasFinal && HasOverride) {
 SourceLocation OverrideLoc = 
Method->getAttr()->getLocation();
 Diag << FixItHint::CreateRemoval(
 CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));

diff  --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
index 082778f2957c..ed163956ecdb 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
@@ -26,7 +26,6 @@ class UseOverrideCheck : public ClangTidyCheck {
 
 private:
   const bool IgnoreDestructors;
-  const bool AllowOverrideAndFinal;
   const std::string OverrideSpelling;
   const std::string FinalSpelling;
 };

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b9feee29d137..4e773d62bf6b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -170,12 +170,6 @@ Improvements to clang-tidy
   Finds non-static member functions that can be made ``const``
   because the functions don't use ``this`` in a non-const way.
 
-- Improved :doc:`modernize-use-override
-  ` check.
-
-  The check now supports the ``AllowOverrideAndFinal`` option to eliminate
-  conflicts with ``gcc -Wsuggest-override`` or ``gcc 
-Werror=suggest-override``.
-
 - The :doc:`readability-redundant-string-init
   ` check now supports a
   `StringNames` option enabling its application to custom string classes.

diff  --git 
a/clang-tools-extra/docs/clang-tidy/checks/modernize-use-override.rst 
b/clang-tools-extra/docs/clang-tidy/checks/modernize-use-override.rst
index d20c1d88520e..4273c6e57708 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize-use-override.rst
+++ b/clang-tools-extra/docs/clang-ti

[clang-tools-extra] 06f3dab - [clang-tidy] Fix readability-redundant-string-init for c++17/c++2a

2019-11-16 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-15T18:09:42-05:00
New Revision: 06f3dabe4a2e85a32ade27c0769b6084c828a206

URL: 
https://github.com/llvm/llvm-project/commit/06f3dabe4a2e85a32ade27c0769b6084c828a206
DIFF: 
https://github.com/llvm/llvm-project/commit/06f3dabe4a2e85a32ade27c0769b6084c828a206.diff

LOG: [clang-tidy] Fix readability-redundant-string-init for c++17/c++2a

Summary:
`readability-redundant-string-init` was one of several clang-tidy checks 
documented as failing for C++17. (The failure mode in C++17 is that it changes 
`std::string Name = ""`; to `std::string Name = Name;`, which actually compiles 
but crashes at run-time.)

Analyzing the AST with `clang -Xclang -ast-dump` showed that the outer 
`CXXConstructExprs` that previously held the correct SourceRange were being 
elided in C++17/2a, but the containing `VarDecl` expressions still had all the 
relevant information. So this patch changes the fix to get its source ranges 
from `VarDecl`.

It adds one test `std::string g = "u", h = "", i = "uuu", j = "", k;` to 
confirm proper warnings and fixit replacements in a single `DeclStmt` where 
some strings require replacement and others don't. The 
readability-redundant-string-init.cpp and 
readability-redundant-string-init-msvc.cpp tests now pass for C++11/14/17/2a.

Reviewers: gribozavr, etienneb, alexfh, hokein, aaron.ballman, gribozavr2

Patch by: poelmanc

Subscribers: NoQ, MyDeveloperDay, Eugene.Zelenko, dylanmckay, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D69238

Added: 


Modified: 
clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/modernize-use-override.rst

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
index 2f15213dca8c..8ee77ccd16ff 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
@@ -20,11 +20,13 @@ namespace modernize {
 UseOverrideCheck::UseOverrideCheck(StringRef Name, ClangTidyContext *Context)
 : ClangTidyCheck(Name, Context),
   IgnoreDestructors(Options.get("IgnoreDestructors", false)),
+  AllowOverrideAndFinal(Options.get("AllowOverrideAndFinal", false)),
   OverrideSpelling(Options.get("OverrideSpelling", "override")),
   FinalSpelling(Options.get("FinalSpelling", "final")) {}
 
 void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IgnoreDestructors", IgnoreDestructors);
+  Options.store(Opts, "AllowOverrideAndFinal", AllowOverrideAndFinal);
   Options.store(Opts, "OverrideSpelling", OverrideSpelling);
   Options.store(Opts, "FinalSpelling", FinalSpelling);
 }
@@ -103,7 +105,8 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
   bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
   unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
 
-  if (!OnlyVirtualSpecified && KeywordCount == 1)
+  if ((!OnlyVirtualSpecified && KeywordCount == 1) ||
+  (!HasVirtual && HasOverride && HasFinal && AllowOverrideAndFinal))
 return; // Nothing to do.
 
   std::string Message;
@@ -113,8 +116,9 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
 Message = "annotate this function with '%0' or (rarely) '%1'";
   } else {
 StringRef Redundant =
-HasVirtual ? (HasOverride && HasFinal ? "'virtual' and '%0' are"
-  : "'virtual' is")
+HasVirtual ? (HasOverride && HasFinal && !AllowOverrideAndFinal
+  ? "'virtual' and '%0' are"
+  : "'virtual' is")
: "'%0' is";
 StringRef Correct = HasFinal ? "'%1'" : "'%0'";
 
@@ -211,7 +215,7 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult 
&Result) {
 Diag << FixItHint::CreateInsertion(InsertLoc, ReplacementText);
   }
 
-  if (HasFinal && HasOverride) {
+  if (HasFinal && HasOverride && !AllowOverrideAndFinal) {
 SourceLocation OverrideLoc = 
Method->getAttr()->getLocation();
 Diag << FixItHint::CreateRemoval(
 CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));

diff  --git a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h 
b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
index ed163956ecdb..082778f2957c 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
@@ -26,6 +26,7 @@ class UseOverrideCheck : public ClangTidyCheck {
 
 private:
   const bool IgnoreDestructors;
+  const bool AllowOverrideAndFinal;
   const std::string OverrideSpelling;
   con

[clang-tools-extra] 47bd7c5 - Revert "[clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix"

2019-11-16 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-15T16:46:58-05:00
New Revision: 47bd7c57e556e3a4db51566d59c2d796ce898c8f

URL: 
https://github.com/llvm/llvm-project/commit/47bd7c57e556e3a4db51566d59c2d796ce898c8f
DIFF: 
https://github.com/llvm/llvm-project/commit/47bd7c57e556e3a4db51566d59c2d796ce898c8f.diff

LOG: Revert "[clang-tidy] Give readability-redundant-string-init a customizable 
list of string types to fix"

This reverts commit 96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982.

Added: 


Modified: 
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
clang-tools-extra/docs/ReleaseNotes.rst

clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
index 6bf0edb7231f..a140e17fabfd 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -8,7 +8,6 @@
 
 #include "RedundantStringInitCheck.h"
 #include "../utils/Matchers.h"
-#include "../utils/OptionsUtils.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
@@ -18,43 +17,19 @@ namespace clang {
 namespace tidy {
 namespace readability {
 
-const char DefaultStringNames[] = "::std::basic_string";
-
-RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
-   ClangTidyContext *Context)
-: ClangTidyCheck(Name, Context),
-  StringNames(utils::options::parseStringList(
-  Options.get("StringNames", DefaultStringNames))) {}
-
-void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) 
{
-  Options.store(Opts, "StringNames", DefaultStringNames);
-}
-
 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
-  const auto hasStringTypeName = hasAnyName(
-  SmallVector(StringNames.begin(), StringNames.end()));
-
-  // Version of StringNames with namespaces removed
-  std::vector stringNamesNoNamespace;
-  for (const std::string &name : StringNames) {
-std::string::size_type colonPos = name.rfind(':');
-stringNamesNoNamespace.push_back(
-name.substr(colonPos == std::string::npos ? 0 : colonPos + 1));
-  }
-  const auto hasStringCtorName = hasAnyName(SmallVector(
-  stringNamesNoNamespace.begin(), stringNamesNoNamespace.end()));
 
   // Match string constructor.
-  const auto StringConstructorExpr = expr(
-  anyOf(cxxConstructExpr(argumentCountIs(1),
- hasDeclaration(cxxMethodDecl(hasStringCtorName))),
-// If present, the second argument is the alloc object which must
-// not be present explicitly.
-cxxConstructExpr(argumentCountIs(2),
- hasDeclaration(cxxMethodDecl(hasStringCtorName)),
- hasArgument(1, cxxDefaultArgExpr();
+  const auto StringConstructorExpr = expr(anyOf(
+  cxxConstructExpr(argumentCountIs(1),
+   hasDeclaration(cxxMethodDecl(hasName("basic_string",
+  // If present, the second argument is the alloc object which must not
+  // be present explicitly.
+  cxxConstructExpr(argumentCountIs(2),
+   hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
+   hasArgument(1, cxxDefaultArgExpr();
 
   // Match a string constructor expression with an empty string literal.
   const auto EmptyStringCtorExpr = cxxConstructExpr(
@@ -73,7 +48,7 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder 
*Finder) {
   namedDecl(
   varDecl(
   hasType(hasUnqualifiedDesugaredType(recordType(
-  hasDeclaration(cxxRecordDecl(hasStringTypeName),
+  hasDeclaration(cxxRecordDecl(hasName("basic_string")),
   hasInitializer(expr(ignoringImplicit(anyOf(
   EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)
   .bind("vardecl"),

diff  --git 
a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h 
b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
index 9ab009aea84d..b1f551ed5607 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
@@ -10,8 +10,6 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_STRING_INIT_H
 
 #include "../ClangTidyCheck.h"
-#include 
-#include 
 
 namespace clang {
 namespace tidy {
@@ -20,13 +18,10 @@ namespace readabi

[clang-tools-extra] 96fbc32 - [clang-tidy] Give readability-redundant-string-init a customizable list of string types to fix

2019-11-16 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-15T16:42:54-05:00
New Revision: 96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982

URL: 
https://github.com/llvm/llvm-project/commit/96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982
DIFF: 
https://github.com/llvm/llvm-project/commit/96fbc32cb9ea23b1e7e3ff6906ec3ccda9500982.diff

LOG: [clang-tidy] Give readability-redundant-string-init a customizable list of 
string types to fix

Summary:
This patch adds a feature requested in https://reviews.llvm.org/D69238 to 
enable `readability-redundant-string-init` to take a list of strings to apply 
the fix to rather than hard-coding `basic_string`. It adds a `StringNames` 
option of semicolon-delimited names of string classes to which to apply this 
fix. Tests ensure this works with test class out::TestString as well as 
std::string and std::wstring as before. It should be applicable to 
llvm::StringRef, QString, etc.

Reviewers: MyDeveloperDay, aaron.ballman, hokein, alexfh, JonasToth, gribozavr2

Patch by: poelmanc

Subscribers: gribozavr2, xazax.hun, Eugene.Zelenko, cfe-commits

Tags: #clang-tools-extra, #clang

Differential Revision: https://reviews.llvm.org/D69548

Added: 


Modified: 
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.h
clang-tools-extra/docs/ReleaseNotes.rst

clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
index a140e17fabfd..6bf0edb7231f 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -8,6 +8,7 @@
 
 #include "RedundantStringInitCheck.h"
 #include "../utils/Matchers.h"
+#include "../utils/OptionsUtils.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
@@ -17,19 +18,43 @@ namespace clang {
 namespace tidy {
 namespace readability {
 
+const char DefaultStringNames[] = "::std::basic_string";
+
+RedundantStringInitCheck::RedundantStringInitCheck(StringRef Name,
+   ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  StringNames(utils::options::parseStringList(
+  Options.get("StringNames", DefaultStringNames))) {}
+
+void RedundantStringInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) 
{
+  Options.store(Opts, "StringNames", DefaultStringNames);
+}
+
 void RedundantStringInitCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus)
 return;
+  const auto hasStringTypeName = hasAnyName(
+  SmallVector(StringNames.begin(), StringNames.end()));
+
+  // Version of StringNames with namespaces removed
+  std::vector stringNamesNoNamespace;
+  for (const std::string &name : StringNames) {
+std::string::size_type colonPos = name.rfind(':');
+stringNamesNoNamespace.push_back(
+name.substr(colonPos == std::string::npos ? 0 : colonPos + 1));
+  }
+  const auto hasStringCtorName = hasAnyName(SmallVector(
+  stringNamesNoNamespace.begin(), stringNamesNoNamespace.end()));
 
   // Match string constructor.
-  const auto StringConstructorExpr = expr(anyOf(
-  cxxConstructExpr(argumentCountIs(1),
-   hasDeclaration(cxxMethodDecl(hasName("basic_string",
-  // If present, the second argument is the alloc object which must not
-  // be present explicitly.
-  cxxConstructExpr(argumentCountIs(2),
-   hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
-   hasArgument(1, cxxDefaultArgExpr();
+  const auto StringConstructorExpr = expr(
+  anyOf(cxxConstructExpr(argumentCountIs(1),
+ hasDeclaration(cxxMethodDecl(hasStringCtorName))),
+// If present, the second argument is the alloc object which must
+// not be present explicitly.
+cxxConstructExpr(argumentCountIs(2),
+ hasDeclaration(cxxMethodDecl(hasStringCtorName)),
+ hasArgument(1, cxxDefaultArgExpr();
 
   // Match a string constructor expression with an empty string literal.
   const auto EmptyStringCtorExpr = cxxConstructExpr(
@@ -48,7 +73,7 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder 
*Finder) {
   namedDecl(
   varDecl(
   hasType(hasUnqualifiedDesugaredType(recordType(
-  hasDeclaration(cxxRecordDecl(hasName("basic_string")),
+  hasDeclaration(cxxRecordDecl(hasStringTypeName),
   hasInitializer(expr(ignoringImplicit(anyOf(
   

[clang-tools-extra] 50e9956 - [clang-tidy] modernize-use-override new option AllowOverrideAndFinal

2019-11-16 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-15T16:16:07-05:00
New Revision: 50e99563fb0459f5160572eef3c4e6062b8ad3f2

URL: 
https://github.com/llvm/llvm-project/commit/50e99563fb0459f5160572eef3c4e6062b8ad3f2
DIFF: 
https://github.com/llvm/llvm-project/commit/50e99563fb0459f5160572eef3c4e6062b8ad3f2.diff

LOG: [clang-tidy] modernize-use-override new option AllowOverrideAndFinal

Summary:
In addition to adding `override` wherever possible, clang-tidy's 
`modernize-use-override` nicely removes `virtual` when `override` or `final` is 
specified, and further removes override when final is specified. While this is 
great default behavior, when code needs to be compiled with gcc at high warning 
levels that include `gcc -Wsuggest-override` or `gcc -Werror=suggest-override`, 
clang-tidy's removal of the redundant `override` keyword causes gcc to emit a 
warning or error. This discrepancy / conflict has been noted by others 
including a comment on Stack Overflow and by Mozilla's Firefox developers.

This patch adds an AllowOverrideAndFinal option defaulting to 0 - thus 
preserving current behavior - that when enabled allows both `override` and 
`final` to co-exist, while still fixing all other issues.

The patch includes a test file verifying all combinations of 
virtual/override/final, and mentions the new option in the release notes.

Reviewers: alexfh, djasper, JonasToth

Patch by: poelmanc

Subscribers: JonasToth, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D70165

Added: 


Modified: 
clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp

clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
index 9a9a3830201b..a140e17fabfd 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -46,23 +46,23 @@ void RedundantStringInitCheck::registerMatchers(MatchFinder 
*Finder) {
   // string bar("");
   Finder->addMatcher(
   namedDecl(
-  varDecl(hasType(hasUnqualifiedDesugaredType(recordType(
-  
hasDeclaration(cxxRecordDecl(hasName("basic_string")),
-  hasInitializer(expr(ignoringImplicit(anyOf(
-  EmptyStringCtorExpr,
-  EmptyStringCtorExprWithTemporaries)))
- .bind("expr"))),
-  unless(parmVarDecl()))
-  .bind("decl"),
+  varDecl(
+  hasType(hasUnqualifiedDesugaredType(recordType(
+  hasDeclaration(cxxRecordDecl(hasName("basic_string")),
+  hasInitializer(expr(ignoringImplicit(anyOf(
+  EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)
+  .bind("vardecl"),
+  unless(parmVarDecl())),
   this);
 }
 
 void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
-  const auto *CtorExpr = Result.Nodes.getNodeAs("expr");
-  const auto *Decl = Result.Nodes.getNodeAs("decl");
-  diag(CtorExpr->getExprLoc(), "redundant string initialization")
-  << FixItHint::CreateReplacement(CtorExpr->getSourceRange(),
-  Decl->getName());
+  const auto *VDecl = Result.Nodes.getNodeAs("vardecl");
+  // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
+  // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
+  SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
+  diag(VDecl->getLocation(), "redundant string initialization")
+  << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
 }
 
 } // namespace readability

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp
index fe05845db663..0299519a508e 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s 
readability-redundant-string-init %t
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy %s readability-redundant-string-init %t
 
 namespace std {
 template 

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
index b88e4a0f4d62..76d1fe97e1b9 100644
--- 
a/clang-tools-extra/t

[clang-tools-extra] 782392d - [clang-tidy] modernize-use-using work with multi-argument templates

2019-11-16 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-15T16:09:10-05:00
New Revision: 782392db8122cafb5e0e4ad5fe0c24c46f11b2b7

URL: 
https://github.com/llvm/llvm-project/commit/782392db8122cafb5e0e4ad5fe0c24c46f11b2b7
DIFF: 
https://github.com/llvm/llvm-project/commit/782392db8122cafb5e0e4ad5fe0c24c46f11b2b7.diff

LOG: [clang-tidy] modernize-use-using work with multi-argument templates

Summary:
If clang-tidy's modernize-use-using feature finds any commas that are not 
within parentheses, it won't create a fix. That means it won't change lines 
like:
  typedef std::pair Point;
to
  using Point = std::pair;
or even:
  typedef std::map MyMap;
  typedef std::vector> MyVector;

This patch allows the fix to apply to lines with commas if they are within 
parentheses or angle brackets that were not themselves within parentheses.

Reviewers: alexfh, hokein, aaron.ballman

Patch by: poelmanc

Subscribers: jonathanmeier, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D67460

Added: 


Modified: 
clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp

Removed: 




diff  --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp 
b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index bd234623d9ec..c9ca8a6114a5 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -39,25 +39,46 @@ static bool CheckRemoval(SourceManager &SM, SourceLocation 
StartLoc,
   File.begin(), TokenBegin, File.end());
 
   Token Tok;
-  int ParenLevel = 0;
+  int NestingLevel = 0; // Parens, braces, and square brackets
+  int AngleBracketLevel = 0;
   bool FoundTypedef = false;
 
   while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
 switch (Tok.getKind()) {
 case tok::l_brace:
-case tok::r_brace:
-  // This might be the `typedef struct {...} T;` case.
-  return false;
+  if (NestingLevel == 0 && AngleBracketLevel == 0) {
+// At top level, this might be the `typedef struct {...} T;` case.
+// Inside parens, square brackets, or angle brackets it's not.
+return false;
+  }
+  ++NestingLevel;
+  break;
 case tok::l_paren:
-  ParenLevel++;
+case tok::l_square:
+  ++NestingLevel;
   break;
+case tok::r_brace:
 case tok::r_paren:
-  ParenLevel--;
+case tok::r_square:
+  --NestingLevel;
+  break;
+case tok::less:
+  // If not nested in paren/brace/square bracket, treat as opening angle 
bracket.
+  if (NestingLevel == 0)
+++AngleBracketLevel;
+  break;
+case tok::greater:
+  // Per C++ 17 Draft N4659, Section 17.2/3
+  //   https://timsong-cpp.github.io/cppwp/n4659/temp.names#3:
+  // "When parsing a template-argument-list, the first non-nested > is
+  // taken as the ending delimiter rather than a greater-than operator."
+  // If not nested in paren/brace/square bracket, treat as closing angle 
bracket.
+  if (NestingLevel == 0)
+--AngleBracketLevel;
   break;
 case tok::comma:
-  if (ParenLevel == 0) {
-// If there is comma and we are not between open parenthesis then it is
-// two or more declarations in this chain.
+  if (NestingLevel == 0 && AngleBracketLevel == 0) {
+// If there is a non-nested comma we have two or more declarations in 
this chain.
 return false;
   }
   break;
@@ -88,8 +109,7 @@ void UseUsingCheck::check(const MatchFinder::MatchResult 
&Result) {
   if (StartLoc.isMacroID() && IgnoreMacros)
 return;
 
-  auto Diag =
-  diag(StartLoc, "use 'using' instead of 'typedef'");
+  auto Diag = diag(StartLoc, "use 'using' instead of 'typedef'");
 
   // do not fix if there is macro or array
   if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
index efa4030d001e..73fd2701bbdf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-using.cpp
@@ -183,3 +183,67 @@ class E : public C {
   void f() override { super::f(); }
 };
 }
+
+template 
+class TwoArgTemplate {
+  typedef TwoArgTemplate self;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef'
+  // CHECK-FIXES: using self = TwoArgTemplate;
+};
+
+template 
+struct S {};
+
+typedef S<(0 > 0), int> S_t, *S_p;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: typedef S<(0 > 0), int> S_t, *S_p;
+
+typedef S<(0 < 0), int> S2_t, *S2_p;
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
+// CH

[clang] 4ee70e0 - [clang-format] Fixed edge-case with SpacesInSquareBrackets with trailing bare "&" lambda capture.

2019-11-14 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-11-14T13:24:50-05:00
New Revision: 4ee70e00b509fe26bac4196df76dc7c6153f1206

URL: 
https://github.com/llvm/llvm-project/commit/4ee70e00b509fe26bac4196df76dc7c6153f1206
DIFF: 
https://github.com/llvm/llvm-project/commit/4ee70e00b509fe26bac4196df76dc7c6153f1206.diff

LOG: [clang-format] Fixed edge-case with SpacesInSquareBrackets with trailing 
bare "&" lambda capture.

Summary:
Lambda captures allow for a lone `&` capture, so `&]` needs to be properly 
handled.

`int foo = [& ]() {}` is fixed to give `int foo = [ & ]() {}`

Reviewers: MyDeveloperDay

Reviewed by: MyDeveloperDay

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D70249

Added: 


Modified: 
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index d3c9dd56f97c..c5ed49e9a409 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2996,14 +2996,18 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine &Line,
 return true;
   }
   if (Left.is(TT_UnaryOperator)) {
-// The alternative operators for ~ and ! are "compl" and "not".
-// If they are used instead, we do not want to combine them with
-// the token to the right, unless that is a left paren.
 if (!Right.is(tok::l_paren)) {
+  // The alternative operators for ~ and ! are "compl" and "not".
+  // If they are used instead, we do not want to combine them with
+  // the token to the right, unless that is a left paren.
   if (Left.is(tok::exclaim) && Left.TokenText == "not")
 return true;
   if (Left.is(tok::tilde) && Left.TokenText == "compl")
 return true;
+  // Lambda captures allow for a lone &, so "&]" needs to be properly
+  // handled.
+  if (Left.is(tok::amp) && Right.is(tok::r_square))
+return Style.SpacesInSquareBrackets;
 }
 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
Right.is(TT_BinaryOperator);

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 01154e2c2f40..b8a73621c779 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10638,6 +10638,7 @@ TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
   verifyFormat("return [ i, args... ] {};", Spaces);
   verifyFormat("int foo = [ &bar ]() {};", Spaces);
   verifyFormat("int foo = [ = ]() {};", Spaces);
+  verifyFormat("int foo = [ & ]() {};", Spaces);
   verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
   verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
 }



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 8d7bd57 - [clang-format] Fix SpacesInSquareBrackets for Lambdas with Initial "&ref" Parameter

2019-10-31 Thread Mitchell Balan via cfe-commits

Author: Mitchell Balan
Date: 2019-10-31T11:08:05-04:00
New Revision: 8d7bd57526486cab9e3daba9934042c405d7946b

URL: 
https://github.com/llvm/llvm-project/commit/8d7bd57526486cab9e3daba9934042c405d7946b
DIFF: 
https://github.com/llvm/llvm-project/commit/8d7bd57526486cab9e3daba9934042c405d7946b.diff

LOG: [clang-format] Fix SpacesInSquareBrackets for Lambdas with Initial "&ref" 
Parameter

Summary:
This fixes an edge case in the `SpacesInSquareBrackets` option where an initial 
`&ref` lambda parameter is not padded with an initial space.

`int foo = [&bar ]() {}` is fixed to give `int foo = [ &bar ]() {}`

Reviewers: MyDeveloperDay, klimek, sammccall

Reviewed by: MyDeveloperDay

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D69649

Added: 


Modified: 
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

Removed: 




diff  --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 1ed35597d075..98b87c0f5a25 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2567,7 +2567,7 @@ bool TokenAnnotator::spaceRequiredBetween(const 
AnnotatedLine &Line,
 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous 
&&
 Left.Previous->is(tok::kw_case));
   if (Left.is(tok::l_square) && Right.is(tok::amp))
-return false;
+return Style.SpacesInSquareBrackets;
   if (Right.is(TT_PointerOrReference)) {
 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
   if (!Left.MatchingParen)

diff  --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index e0ebef1f7ced..ea03ee1c3cc9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10572,6 +10572,10 @@ TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) 
{
   // Lambdas.
   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
   verifyFormat("return [ i, args... ] {};", Spaces);
+  verifyFormat("int foo = [ &bar ]() {};", Spaces);
+  verifyFormat("int foo = [ = ]() {};", Spaces);
+  verifyFormat("int foo = [ =, &bar ]() {};", Spaces);
+  verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
 }
 
 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r374175 - [NFC] Reverting changes from test commit.

2019-10-09 Thread Mitchell Balan via cfe-commits
Author: mitchell
Date: Wed Oct  9 08:12:22 2019
New Revision: 374175

URL: http://llvm.org/viewvc/llvm-project?rev=374175&view=rev
Log:
[NFC] Reverting changes from test commit.
llvm commit access test succeeded.

Modified:
cfe/trunk/docs/ClangFormatStyleOptions.rst

Modified: cfe/trunk/docs/ClangFormatStyleOptions.rst
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormatStyleOptions.rst?rev=374175&r1=374174&r2=374175&view=diff
==
--- cfe/trunk/docs/ClangFormatStyleOptions.rst (original)
+++ cfe/trunk/docs/ClangFormatStyleOptions.rst Wed Oct  9 08:12:22 2019
@@ -2507,5 +2507,3 @@ The result is:
   r();
   }
   }
-
-  
\ No newline at end of file


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r374174 - [NFC] Test commit.

2019-10-09 Thread Mitchell Balan via cfe-commits
Author: mitchell
Date: Wed Oct  9 08:11:34 2019
New Revision: 374174

URL: http://llvm.org/viewvc/llvm-project?rev=374174&view=rev
Log:
[NFC] Test commit.
Testing llvm commit access only.

Modified:
cfe/trunk/docs/ClangFormatStyleOptions.rst

Modified: cfe/trunk/docs/ClangFormatStyleOptions.rst
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormatStyleOptions.rst?rev=374174&r1=374173&r2=374174&view=diff
==
--- cfe/trunk/docs/ClangFormatStyleOptions.rst (original)
+++ cfe/trunk/docs/ClangFormatStyleOptions.rst Wed Oct  9 08:11:34 2019
@@ -2507,3 +2507,5 @@ The result is:
   r();
   }
   }
+
+  
\ No newline at end of file


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits