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