sammccall created this revision.
sammccall added a reviewer: hokein.
Herald added a subscriber: kadircet.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

- remove assert that can fail for input `/\<newline>* */`
- assert was also checking the wrong condition: that the prefix *differed* from 
either `//` or from `/*`. Avoid use of strncmp where we can.
- add a comment that the brittleness of the text matching is intentional


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138780

Files:
  clang-tools-extra/include-cleaner/lib/Record.cpp


Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -144,9 +144,13 @@
 // FIXME: this is a mirror of clang::clangd::parseIWYUPragma, move to 
libTooling
 // to share the code?
 static llvm::Optional<StringRef> parseIWYUPragma(const char *Text) {
-  assert(strncmp(Text, "//", 2) || strncmp(Text, "/*", 2));
+  // Skip the comment start, // or /*.
+  if (Text[0] != '/' || (Text[1] != '/' && Text[1] != '*'))
+    return llvm::None;
+  Text += 2;
+
+  // Per spec, direcitves are whitespace- and case-sensitive.
   constexpr llvm::StringLiteral IWYUPragma = " IWYU pragma: ";
-  Text += 2; // Skip the comment start, // or /*.
   if (strncmp(Text, IWYUPragma.data(), IWYUPragma.size()))
     return llvm::None;
   Text += IWYUPragma.size();


Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -144,9 +144,13 @@
 // FIXME: this is a mirror of clang::clangd::parseIWYUPragma, move to libTooling
 // to share the code?
 static llvm::Optional<StringRef> parseIWYUPragma(const char *Text) {
-  assert(strncmp(Text, "//", 2) || strncmp(Text, "/*", 2));
+  // Skip the comment start, // or /*.
+  if (Text[0] != '/' || (Text[1] != '/' && Text[1] != '*'))
+    return llvm::None;
+  Text += 2;
+
+  // Per spec, direcitves are whitespace- and case-sensitive.
   constexpr llvm::StringLiteral IWYUPragma = " IWYU pragma: ";
-  Text += 2; // Skip the comment start, // or /*.
   if (strncmp(Text, IWYUPragma.data(), IWYUPragma.size()))
     return llvm::None;
   Text += IWYUPragma.size();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D138780: [include-clean... Sam McCall via Phabricator via cfe-commits

Reply via email to