[PATCH] D142804: [clang-format] Support clang-format on/off line comments as prefixes

Alex via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 29 21:11:20 PST 2023


alexolog added inline comments.


================
Comment at: clang/lib/Format/Format.cpp:3893
 
+bool isClangFormatOn(StringRef Comment) {
+  if (Comment == "/* clang-format on */")
----------------
owenpan wrote:
> alexolog wrote:
> > alexolog wrote:
> > > Here's my attempt at something flexible:
> > > Disclaimer: this is my first time looking at the LLVM codebase, so don't expect production quality.
> > > 
> > > ```
> > > ////////////////////////////////////////////////////////////////////////////////////////////////////
> > > 
> > > // Implementation detail, hide in a namespace and/or take out of the header
> > > bool isClangFormatMarked(StringRef Comment, StringRef Mark) {
> > >   // Quick heuristics: minimum length and starts with a slash (comment)
> > >   // Shortest tag: "//clang-format on", 17 characters
> > >   static constexpr StringLiteral clangFormatStr("clang-format ");
> > >   if (Comment.size() < clangFormatStr.size() + 4 || Comment[0] != '/')
> > >     return false;
> > > 
> > >   // check if it's a comment starting with "//" or "/*"
> > >   bool CloseNeeded = false;
> > >   if (Comment[1] == '*')
> > >     CloseNeeded = true;
> > >   else if (Comment[1] != '/')
> > >     return false;
> > > 
> > >   // Remove the comment start and all following whitespace
> > >   Comment = Comment.substr(2).ltrim();
> > > 
> > >   // Check for the actual command, a piece at a time
> > >   if (!Comment.consume_front(clangFormatStr) || !Comment.consume_front(Mark))
> > >     return false;
> > > 
> > >   // Are we there yet?
> > >   if (!CloseNeeded && Comment.empty() ||
> > >       CloseNeeded && Comment.starts_with("*/"))
> > >     return true;
> > > 
> > >   // For a trailer, restrict the next character
> > >   // (currently spaces and tabs, but can include a colon, etc.)
> > >   static constexpr StringLiteral Separator(" \t");
> > >   if (!Separator.contains(Comment[0]))
> > >     return false;
> > >   
> > >   // Verify comment is properly terminated
> > >   if (!CloseNeeded || Comment.contains("*/"))
> > >     return true;
> > > 
> > >   return false; // Everything else
> > > }
> > > 
> > > ////////////////////////////////////////////////////////////////////////////////////////////////////
> > > 
> > > bool isClangFormatOn(StringRef Comment) {
> > >   return isClangFormatMarked(Comment, "on");
> > > }
> > > 
> > > bool isClangFormatOff(StringRef Comment) {
> > >   return isClangFormatMarked(Comment, "off");
> > > }
> > > 
> > > ////////////////////////////////////////////////////////////////////////////////////////////////////
> > > 
> > >   EXPECT_TRUE(isClangFormatOn("//clang-format on"));
> > >   EXPECT_TRUE(isClangFormatOn("// clang-format on"));
> > >   EXPECT_TRUE(isClangFormatOn("//clang-format on "));
> > >   EXPECT_TRUE(isClangFormatOn("//clang-format on and off"));
> > >   EXPECT_TRUE(isClangFormatOn("/*clang-format on*/"));
> > >   EXPECT_TRUE(isClangFormatOn("/* clang-format on*/"));
> > >   EXPECT_TRUE(isClangFormatOn("/*clang-format on */"));
> > >   EXPECT_TRUE(isClangFormatOn("/*clang-format on*/int i{0};"));
> > > 
> > >   EXPECT_FALSE(isClangFormatOn("//clang-format  on"));
> > >   EXPECT_FALSE(isClangFormatOn("//clang-format o"));
> > >   EXPECT_FALSE(isClangFormatOn("//     clang-format o"));
> > >   EXPECT_FALSE(isClangFormatOn("//clang-format ontario"));
> > >   EXPECT_FALSE(isClangFormatOn("//clang-format off"));
> > >   EXPECT_FALSE(isClangFormatOn("/*clang-format onwards*/"));
> > > 
> > > ////////////////////////////////////////////////////////////////////////////////////////////////////
> > > ```
> > Sorry about the "done".  My misunderstanding
> > Here's my attempt at something flexible:
> > Disclaimer: this is my first time looking at the LLVM codebase, so don't expect production quality.
> 
> Thanks! If we didn't have to worry about regressions, we might want to do something like what you suggested above.
Isn't it what extensive test coverage is for?


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

https://reviews.llvm.org/D142804



More information about the cfe-commits mailing list