[clang] [clang-tools-extra] [clang] Don't add documentation comments to the AST if not requested (PR #206363)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 20 06:34:09 PDT 2026


================
@@ -2726,11 +2726,39 @@ LambdaScopeInfo *Sema::getCurGenericLambda() {
   return nullptr;
 }
 
+bool Sema::shouldRetainCommentsInAST(SourceLocation Loc) const {
+  if (LangOpts.CommentOpts.ParseAllComments)
+    return true;
+
+  if (LangOpts.CommentOpts.RetainComments)
+    return true;
+
+  // When building a PCH the comments are serialized into the AST file
+  // so downstream consumers like clangd) can retrieve documentation, and the
+  // incremental/REPL front end may query them interactively.
+  if (TUKind != TU_Complete)
+    return true;
+
+  if (PP.isCodeCompletionEnabled())
+    return true;
+
+  // Keep the comment if any of the -Wdocumentation warnings is enabled at
+  // its location (checking the location handles warnings turned on by
+  // `#pragma clang diagnostic`). -Wdocumentation-pedantic is checked
+  // separately because it is not a subgroup of -Wdocumentation.
+  if (!Diags.areAllIgnored("documentation", Loc) ||
+      !Diags.areAllIgnored("documentation-pedantic", Loc))
+    return true;
+
+  return false;
+}
 
 void Sema::ActOnComment(SourceRange Comment) {
-  if (!LangOpts.RetainCommentsFromSystemHeaders &&
+  if (!LangOpts.CommentOpts.RetainCommentsFromSystemHeaders &&
----------------
erichkeane wrote:

Is there a reason `shouldREtainCommentsInAST` shouldn't be checking this as well? 

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


More information about the cfe-commits mailing list