[clang] [APINotes] Match function-like Where.Parameters in Sema (PR #205307)
John Hui via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 2 17:59:43 PDT 2026
================
@@ -993,6 +993,87 @@ UnwindTagContext(TagDecl *DC, api_notes::APINotesManager &APINotes) {
return std::nullopt;
}
+static void stripAPINotesParameterNullability(QualType &ParamType) {
+ while (true) {
+ if (!AttributedType::stripOuterNullability(ParamType))
+ return;
+ }
+}
+
+// Print the APINotes selector spelling for one parameter. The source-spelled
+// selector is tried first. The desugared spelling is only a permissive
+// fallback.
+static std::string getAPINotesParameterSelectorSpelling(
+ QualType ParamType, const ASTContext &Context, const PrintingPolicy &Policy,
+ bool Desugar) {
+ ParamType.removeLocalConst();
+ stripAPINotesParameterNullability(ParamType);
+
+ if (Desugar) {
+ ParamType = ParamType.getDesugaredType(Context);
+ ParamType.removeLocalConst();
+ stripAPINotesParameterNullability(ParamType);
+ }
+
+ return ParamType.getAsString(Policy);
+}
+
+static std::optional<SmallVector<SmallVector<std::string, 4>, 2>>
+getAPINotesParameterSelectorCandidates(const Sema &S, const FunctionDecl *FD) {
+ const auto *FPT = FD->getType()->getAs<FunctionProtoType>();
+ if (!FPT)
+ return std::nullopt;
+
+ SmallVector<std::string, 4> SourceParameters;
+ SmallVector<std::string, 4> DesugaredParameters;
+ SourceParameters.reserve(FPT->getNumParams());
+ DesugaredParameters.reserve(FPT->getNumParams());
+
+ const PrintingPolicy &Policy = S.Context.getPrintingPolicy();
----------------
j-hui wrote:
We're doing string comparisons, which is sensitive to whitespace and other formatting details that I assume come from this printing policy. I'm concerned that this policy can be affected by things like command-line arguments and lead to unexpected mismatches. I'm thinking of really trivial scenarios like `A<B,C>` failing to match against `A<B, C>`, or `operator +` failing to match against `operator+`. I assume there are more complex scenarios to consider too, though we can draw the line somewhere and just say everything else is out of scope.
I'm wondering whether (1) we should be using some kind of fixed printing policy that ensure the spelling is stable, and (2) whether we should also do some normalization on the strings before we compare them, e.g., replace all whitespace characters with ` `, collapsing consecutive spaces, trimming leading and trailing whitespace, etc.?
https://github.com/llvm/llvm-project/pull/205307
More information about the cfe-commits
mailing list