I'm currently implementing a clang tool. I have no idea how to find out the noexcept-ness of a function given its clang::FunctionDecl. For the following code
struct X {
X() = default;
};
int main() {
X x;
}
clang-query shows that the default constructor of X is noexcept:
-CXXConstructorDecl 0x563b8aea50e8 <line:2:3, col:15> col:3 used constexpr X 'void () noexcept' default trivial implicit-inline
`-CompoundStmt 0x563b8aea5768 <col:15>
but fd->getExceptionSpecType() returns clang::ExceptionSpecificationType::EST_None. Moreover, I think the noexcept-ness might also be encoded in the function type (since it is part of the function type since C++17), but I don't know how to extract that information.
To get the
ExceptionSpecificationTypeof aFunctionDecl, callValueDecl::getType()to get itsQualType, callgetTypePtr()on that to get itsType, then usedyn_castto downcast that to aFunctionProtoType, and finally callFunctionProtoType::getExceptionSpecType().For example:
On your example, for
X::X(), the above yieldsEST_BasicNoexcept.(I'm using Clang+LLVM-16.0.0 on Linux, but this should be the same across a fairly wide range of versions.)
Why doesn't FunctionDecl::getExceptionSpecType work?
The documentation for
FunctionDecl::getExceptionSpecType()says:The "as declared" part means it only includes
noexceptif that was syntactically present.In contrast, by going through the
FunctionProtoType, we get implicit exception specifications too.Caveat: Unevaluated exception specifications
If you remove the
mainfunction from the example, then the above snippet instead yieldsEST_Unevaluated, whose documentation says:Digging through the sources (e.g.,
SemaExceptionSpec.cpp:1056, etc.), it seems that implicit exception specifications for constructors and the like are not computed until needed. A call site is a "need", as is a virtual override, perhaps among a few other cases.Supplementary
If you want to go deeper on exception specifications, you may want to look at
ExceptionAnalyzer.cppin the Clang sources, since that does a bunch of analysis of exception specification semantics.