[PATCH] D94955: [clang-format] Treat ForEachMacros as loops

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 2 08:20:43 PST 2021


MyDeveloperDay added a comment.

I guess I'm not making myself clear, I was just hoping you could be super explicit about what you are changing so anyone coming into this review would understand why the behaviour had changed (this is just pseudo code it may not be correct for all cases, but you should get the idea), We need to cover the `SBS_Empty`,`SBS_Always`,`SBS_Never` cases and the ShortLoops `true/false`

Add a normal for loop to the `verifyFormat` so we can see that `FOREACH` is being treated exactly the same as as a `for(;;)` loop

  FormatStyle Style = getLLVMStyle();
  // given that LLVM style is...
  // AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
  // AllowShortLoopsOnASingleLine = false;
  
  // This test ensures that foreach blocks are treated exactly the same as their for(;;) counterparts.
  EXPECT_EQ(Style.AllowShortBlocksOnASingleLine,FormatStyle::SBS_Never)
  EXPECT_EQ(Style.AllowShortLoopsOnASingleLine ,true)
  
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++) {\n}\n"
                 "  foreach (Item *item, itemlist) {\n}\n"
                 "  Q_FOREACH (Item *item, itemlist) {\n}\n"
                 "  BOOST_FOREACH (Item *item, itemlist) {\n}\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist) {\n}\n"
                 "}",Style);
  
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++) {\n  std::cout << "loop"; }\n"
                 "  foreach (Item *item, itemlist) {\n  std::cout << "loop"; }\n"
                 "  Q_FOREACH (Item *item, itemlist) {\n  std::cout << "loop";}\n"
                 "  BOOST_FOREACH (Item *item, itemlist) {\n  std::cout << "loop";}\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist) {\n std::cout << "loop";}\n"
                 "}",Style);
  
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++)\n  std::cout << "loop";\n"
                 "  foreach (Item *item, itemlist)\n  std::cout << "loop"; \n"
                 "  Q_FOREACH (Item *item, itemlist)\n  std::cout << "loop";\n"
                 "  BOOST_FOREACH (Item *item, itemlist)\n  std::cout << "loop";\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist)\n std::cout << "loop";\n"
                 "}",Style);
  
  
  Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
  Style.AllowShortLoopsOnASingleLine = false;
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++) {}\n"
                 "  foreach (Item *item, itemlist) {}\n"
                 "  Q_FOREACH (Item *item, itemlist) {}\n"
                 "  BOOST_FOREACH (Item *item, itemlist) {}\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
                 "}",Style);
  
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++) {std::cout << "Loop";}\n"
                 "  foreach (Item *item, itemlist) { std::cout << "Loop";}\n"
                 "  Q_FOREACH (Item *item, itemlist) { std::cout << "Loop";}\n"
                 "  BOOST_FOREACH (Item *item, itemlist) { std::cout << "Loop";}\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist) { std::cout << "Loop";}\n"
                 "}",Style);
  
  verifyFormat("void f() {\n"
                 "  for(int i=0;i<10;i++) std::cout << "Loop";\n"
                 "  foreach (Item *item, itemlist) std::cout << "Loop";\n"
                 "  Q_FOREACH (Item *item, itemlist) std::cout << "Loop";\n"
                 "  BOOST_FOREACH (Item *item, itemlist) std::cout << "Loop";\n"
                 "  UNKNOWN_FORACH(Item * item, itemlist) std::cout << "Loop";\n"
                 "}",Style);




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94955/new/

https://reviews.llvm.org/D94955



More information about the cfe-commits mailing list