@@ -2114,3 +2114,113 @@ module DuckTyping {
21142114 f .getADecorator ( ) .( Name ) .getId ( ) = "property"
21152115 }
21162116}
2117+
2118+ /**
2119+ * Provides a class hierarchy for exception types, covering both builtin
2120+ * exceptions (from typeshed models) and user-defined exception classes.
2121+ */
2122+ module ExceptionTypes {
2123+ private import semmle.python.ApiGraphs
2124+ private import semmle.python.frameworks.data.internal.ApiGraphModels
2125+
2126+ /** Holds if `name` is a builtin exception class name. */
2127+ predicate builtinException ( string name ) {
2128+ typeModel ( "builtins.BaseException~Subclass" , "builtins." + name , "" )
2129+ }
2130+
2131+ /** Holds if builtin exception `sub` is a direct subclass of builtin exception `base`. */
2132+ private predicate builtinExceptionSubclass ( string base , string sub ) {
2133+ typeModel ( "builtins." + base + "~Subclass" , "builtins." + sub , "" )
2134+ }
2135+
2136+ /** An exception type, either a builtin exception or a user-defined exception class. */
2137+ newtype TExceptType =
2138+ /** A user-defined exception class. */
2139+ TUserExceptType ( Class c ) or
2140+ /** A builtin exception class, identified by name. */
2141+ TBuiltinExceptType ( string name ) { builtinException ( name ) }
2142+
2143+ /** An exception type, either a builtin exception or a user-defined exception class. */
2144+ class ExceptType extends TExceptType {
2145+ /** Gets the name of this exception type. */
2146+ string getName ( ) { none ( ) }
2147+
2148+ /** Gets a data-flow node that refers to this exception type. */
2149+ DataFlow:: Node getAUse ( ) { none ( ) }
2150+
2151+ /** Gets a direct superclass of this exception type. */
2152+ ExceptType getADirectSuperclass ( ) { none ( ) }
2153+
2154+ /** Gets a string representation of this exception type. */
2155+ string toString ( ) { result = this .getName ( ) }
2156+
2157+ /**
2158+ * Holds if this element is at the specified location.
2159+ * The location spans column `startColumn` of line `startLine` to
2160+ * column `endColumn` of line `endLine` in file `filepath`.
2161+ * For more information, see
2162+ * [Providing locations in CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
2163+ */
2164+ predicate hasLocationInfo (
2165+ string filePath , int startLine , int startColumn , int endLine , int endColumn
2166+ ) {
2167+ none ( )
2168+ }
2169+ }
2170+
2171+ /** A user-defined exception class. */
2172+ class UserExceptType extends ExceptType , TUserExceptType {
2173+ Class cls ;
2174+
2175+ UserExceptType ( ) { this = TUserExceptType ( cls ) }
2176+
2177+ /** Gets the underlying class. */
2178+ Class asClass ( ) { result = cls }
2179+
2180+ override string getName ( ) { result = cls .getName ( ) }
2181+
2182+ override DataFlow:: Node getAUse ( ) { result = classTracker ( cls ) }
2183+
2184+ override ExceptType getADirectSuperclass ( ) {
2185+ result .( UserExceptType ) .asClass ( ) = getADirectSuperclass ( cls )
2186+ or
2187+ result .( BuiltinExceptType ) .getAUse ( ) .asExpr ( ) = cls .getABase ( )
2188+ }
2189+
2190+ override predicate hasLocationInfo (
2191+ string filePath , int startLine , int startColumn , int endLine , int endColumn
2192+ ) {
2193+ cls .getLocation ( ) .hasLocationInfo ( filePath , startLine , startColumn , endLine , endColumn )
2194+ }
2195+ }
2196+
2197+ /** A builtin exception class, identified by name. */
2198+ class BuiltinExceptType extends ExceptType , TBuiltinExceptType {
2199+ string name ;
2200+
2201+ BuiltinExceptType ( ) { this = TBuiltinExceptType ( name ) }
2202+
2203+ /** Gets the builtin name. */
2204+ string asBuiltinName ( ) { result = name }
2205+
2206+ override string getName ( ) { result = name }
2207+
2208+ override DataFlow:: Node getAUse ( ) { API:: builtin ( name ) .asSource ( ) .flowsTo ( result ) }
2209+
2210+ override ExceptType getADirectSuperclass ( ) {
2211+ builtinExceptionSubclass ( result .( BuiltinExceptType ) .asBuiltinName ( ) , name ) and
2212+ result != this
2213+ }
2214+
2215+ override predicate hasLocationInfo (
2216+ string filePath , int startLine , int startColumn , int endLine , int endColumn
2217+ ) {
2218+ filePath = "" and
2219+ startLine = 0 and
2220+ startColumn = 0 and
2221+ endLine = 0 and
2222+ endColumn = 0
2223+ }
2224+ }
2225+ }
2226+
0 commit comments