[clang] [clang][CUDA/HIP] Fix parsing of `operator<<<...>` (PR #218384)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 25 04:20:53 PDT 2026


================
@@ -2485,6 +2485,30 @@ bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext,
       Actions.CodeCompletion().CodeCompleteOperatorName(getCurScope());
       return true;
     }
+    case tok::lesslessless: {
+      // For CUDA, the Lexer will greedily merge all three <<< in operator<<<
+      // which, in fact, can be a valid template specialization of operator<<,
+      // and will never be a valid kernel launch expression, so split.
+      bool CachingTokens = PP.IsPreviousCachedToken(Tok);
+      // If there was a cache, we should update it when doing token split.
+      // The code below never does.
+      assert(!CachingTokens && "No cache expected");
+
+      SourceLocation TokLoc = Tok.getLocation();
+      unsigned LessLessLength = Lexer::getTokenPrefixLength(
+          TokLoc, /*CharNo=*/2, PP.getSourceManager(), getLangOpts());
+
+      SourceLocation LessLoc = PP.SplitToken(TokLoc, LessLessLength);
+      unsigned OldLength = Tok.getLength();
+
+      Tok.setKind(tok::less);
+      Tok.setLength(OldLength - LessLessLength);
+      Tok.setLocation(LessLoc);
----------------
Fznamznon wrote:

Yes, my bad. 

>  Please also add a source-location test that checks the template < points to the third character.

I added a test case to make sure `<` has the right location. I used diagnostic emission to check that. This is slightly weird and uses FileCheck because -verify doesn't capture columns, only lines. Please let me know if that will work.

https://github.com/llvm/llvm-project/pull/218384


More information about the cfe-commits mailing list