[Mlir-commits] [mlir] [mlir][AsmParser] Fix comment detection in string literals (PR #174125)
Mehdi Amini
llvmlistbot at llvm.org
Wed Jan 14 07:14:06 PST 2026
================
@@ -198,6 +198,43 @@ InFlightDiagnostic Parser::emitError(const Twine &message) {
return emitError(SMLoc::getFromPointer(loc.getPointer() - 1), message);
}
+/// Find the start of a line comment (`//`) in the given string, ignoring
+/// occurrences inside string literals. Returns StringRef::npos if no comment
+/// is found.
+static size_t findCommentStart(StringRef line) {
+ // Fast path: no comment in line at all.
+ size_t slashPos = line.find("//");
+ if (slashPos == StringRef::npos)
+ return StringRef::npos;
+
+ // Fast path: no quote before the '//', so it's a real comment.
+ size_t quotePos = line.find('"');
+ if (quotePos == StringRef::npos || quotePos > slashPos)
+ return slashPos;
+
+ // A quote appears before '//'. Parse carefully to handle string literals.
+ bool inString = false;
+ for (size_t i = 0, e = line.size(); i < e; ++i) {
+ char c = line[i];
+ if (inString) {
+ // Skip escaped characters inside strings.
+ if (c == '\\' && i + 1 < e) {
----------------
joker-eph wrote:
Why the `i + 1 < e` condition here?
https://github.com/llvm/llvm-project/pull/174125
More information about the Mlir-commits
mailing list