Clang tool. Check if a type satisfy a concept


I am writing a Clang tool.

I need a function that given
a concept C, a type T, and a CompilerInstance,
returns whether C is true/false.

The following code appears to work correctly, but

  • is this a proper way of doing it or is there a simpler way ?
  • In particular, I have to use an ugly const_cast in clang 16.
    What is the first argument (decl) in MultiLevelTemplateArgumentList constructor ?
bool satisfy_concept(clang::ConceptDecl const *cpt, 
                                  clang::QualType const &ty, 
                                  clang::CompilerInstance *ci) { 

  // ...
  llvm::SmallVector<const clang::Expr *, 1> constraint_exprs{cpt->getConstraintExpr()};
  llvm::SmallVector<clang::TemplateArgument, 1> targs{{ty}};
  clang::ConstraintSatisfaction s;

  clang::MultiLevelTemplateArgumentList targs_list{const_cast<clang::ConceptDecl *>(cpt), targs, true}; 
  bool error = ci->getSema().CheckConstraintSatisfaction(cpt, constraint_exprs, targs_list, 
                                                         cpt->getSourceRange(), s);

  // in clang 15, one could just say
  // bool error = ci->getSema().CheckConstraintSatisfaction(cpt, constraint_exprs, targs, 
  //                                                        cpt->getSourceRange(), s);
  return s.IsSatisfied;

I think that is more or less right, that should do what you want. No real way around the const-cast unfortunately.

The first argument of the MultiLevelTemplateArgumentList is the declaration that is associated with it, used for ‘sugared’ diagnostics.