Skip to content

Commit

Permalink
Using WarningDescription type to get warning number
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin521 committed Nov 1, 2024
1 parent 0f4a215 commit b3a1288
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 64 deletions.
84 changes: 48 additions & 36 deletions src/Compiler/Driver/CompilerConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Features
open FSharp.Compiler.IO
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Xml
open FSharp.Compiler.TypedTree
open FSharp.Compiler.BuildGraph
open System.Text.RegularExpressions

#if !NO_TYPEPROVIDERS
open FSharp.Core.CompilerServices
Expand Down Expand Up @@ -97,44 +97,56 @@ type WarningNumberSource =
| CommandLineOption
| CompilerDirective

let GetWarningNumber (m, numStr: string, langVersion: LanguageVersion, source: WarningNumberSource) =
[<RequireQualifiedAccess>]
type WarningDescription =
| Int32 of int
| String of string
| Ident of Ident

let GetWarningNumber (m, description: WarningDescription, langVersion: LanguageVersion, source: WarningNumberSource) =
let argFeature = LanguageFeature.ParsedHashDirectiveArgumentNonQuotes

let warnInvalid () =
warning (Error(FSComp.SR.buildInvalidWarningNumber numStr, m))
let parse (numStr: string) =
let trimPrefix (s: string) =
if s.StartsWithOrdinal "FS" then s[2..] else s

let tryParseIntWithFailAction (s: string) (failAction: unit -> unit) =
match Int32.TryParse s with
| true, n -> Some n
| false, _ ->
failAction ()
None
let tryParseIntWithFailAction (s: string) (failAction: unit -> unit) =
match Int32.TryParse s with
| true, n -> Some n
| false, _ ->
failAction ()
None

if source = WarningNumberSource.CommandLineOption then
let s =
if numStr.StartsWithOrdinal "FS" then
numStr[2..]
else
numStr
let warnInvalid () =
warning (Error(FSComp.SR.buildInvalidWarningNumber numStr, m))

tryParseIntWithFailAction s id
elif langVersion.SupportsFeature(argFeature) then
let s =
if numStr[0] = '"' && numStr[numStr.Length - 1] = '"' then
numStr[1 .. numStr.Length - 2]
else
numStr

let s = if s.StartsWithOrdinal "FS" then s[2..] else s
tryParseIntWithFailAction s warnInvalid
elif numStr[0] <> '"' || numStr[numStr.Length - 1] <> '"' then
errorR (languageFeatureError langVersion argFeature m)
None
elif numStr.StartsWith "\"FS" && numStr[numStr.Length - 1] = '"' then
warnInvalid ()
None
else
tryParseIntWithFailAction numStr id
if source = WarningNumberSource.CommandLineOption then
tryParseIntWithFailAction (trimPrefix numStr) id
elif langVersion.SupportsFeature(argFeature) then
tryParseIntWithFailAction (trimPrefix numStr) warnInvalid
else
tryParseIntWithFailAction numStr id

match description with
| WarningDescription.Int32 n ->
if tryCheckLanguageFeatureAndRecover langVersion argFeature m then
Some n
else
None
| WarningDescription.String s ->
if
source = WarningNumberSource.CompilerDirective
&& not (langVersion.SupportsFeature argFeature)
&& s.StartsWithOrdinal "FS"
then
warning (Error(FSComp.SR.buildInvalidWarningNumber s, m))

parse s
| WarningDescription.Ident ident ->
if tryCheckLanguageFeatureAndRecover langVersion argFeature m then
parse ident.idText
else
None

let ComputeMakePathAbsolute implicitIncludeDir (path: string) =
try
Expand Down Expand Up @@ -960,7 +972,7 @@ type TcConfigBuilder =
member tcConfigB.TurnWarningOff(m, s: string) =
use _ = UseBuildPhase BuildPhase.Parameter

match GetWarningNumber(m, s, tcConfigB.langVersion, WarningNumberSource.CommandLineOption) with
match GetWarningNumber(m, WarningDescription.String s, tcConfigB.langVersion, WarningNumberSource.CommandLineOption) with
| None -> ()
| Some n ->
// nowarn:62 turns on mlCompatibility, e.g. shows ML compat items in intellisense menus
Expand All @@ -975,7 +987,7 @@ type TcConfigBuilder =
member tcConfigB.TurnWarningOn(m, s: string) =
use _ = UseBuildPhase BuildPhase.Parameter

match GetWarningNumber(m, s, tcConfigB.langVersion, WarningNumberSource.CommandLineOption) with
match GetWarningNumber(m, WarningDescription.String s, tcConfigB.langVersion, WarningNumberSource.CommandLineOption) with
| None -> ()
| Some n ->
// warnon 62 turns on mlCompatibility, e.g. shows ML compat items in intellisense menus
Expand Down
10 changes: 9 additions & 1 deletion src/Compiler/Driver/CompilerConfig.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ open FSharp.Compiler.Diagnostics
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Features
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
open FSharp.Compiler.BuildGraph

Expand Down Expand Up @@ -931,8 +932,15 @@ type WarningNumberSource =
| CommandLineOption
| CompilerDirective

[<RequireQualifiedAccess>]
type WarningDescription =
| Int32 of int
| String of string
| Ident of Ident

val GetWarningNumber:
m: range * numStr: string * langVersion: LanguageVersion * source: WarningNumberSource -> int option
m: range * description: WarningDescription * langVersion: LanguageVersion * source: WarningNumberSource ->
int option

/// Get the name used for FSharp.Core
val GetFSharpCoreLibraryName: unit -> string
Expand Down
20 changes: 10 additions & 10 deletions src/Compiler/Driver/ParseAndCheckInputs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,21 @@ let PostParseModuleSpec (_i, defaultNamespace, isLastCompiland, fileName, intf)
let GetScopedPragmasForHashDirective hd (langVersion: LanguageVersion) =
[
match hd with
| ParsedHashDirective("nowarn", args, m) ->
| ParsedHashDirective("nowarn", args, _) ->
for s in args do
let warningNumber =
let rd =
match s with
| ParsedHashDirectiveArgument.Int32(n, m) ->
GetWarningNumber(m, string n, langVersion, WarningNumberSource.CompilerDirective)
| ParsedHashDirectiveArgument.Ident(s, m) ->
GetWarningNumber(m, s.idText, langVersion, WarningNumberSource.CompilerDirective)
| ParsedHashDirectiveArgument.String(s, _, m) ->
GetWarningNumber(m, $"\"{s}\"", langVersion, WarningNumberSource.CompilerDirective)
| ParsedHashDirectiveArgument.Int32(n, m) -> Some(m, WarningDescription.Int32 n)
| ParsedHashDirectiveArgument.Ident(ident, m) -> Some(m, WarningDescription.Ident ident)
| ParsedHashDirectiveArgument.String(s, _, m) -> Some(m, WarningDescription.String s)
| _ -> None

match warningNumber with
match rd with
| None -> ()
| Some n -> ScopedPragma.WarningOff(m, n)
| Some(m, description) ->
match GetWarningNumber(m, description, langVersion, WarningNumberSource.CompilerDirective) with
| None -> ()
| Some n -> ScopedPragma.WarningOff(m, n)
| _ -> ()
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ module NonStringArgs =
(Error 3350, Line 3, Col 9, Line 3, Col 11, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 4, Col 9, Line 4, Col 15, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 5, Col 9, Line 5, Col 13, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Warning 203, Line 6, Col 9, Line 6, Col 13, """Invalid warning number '"FS"'""");
(Warning 203, Line 7, Col 9, Line 7, Col 17, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 6, Col 9, Line 6, Col 13, "Invalid warning number 'FS'");
(Warning 203, Line 7, Col 9, Line 7, Col 17, "Invalid warning number 'FSBLAH'");
else
(Warning 203, Line 3, Col 9, Line 3, Col 11, "Invalid warning number 'FS'");
(Warning 203, Line 4, Col 9, Line 4, Col 15, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 5, Col 9, Line 5, Col 13, "Invalid warning number 'ACME'");
(Warning 203, Line 6, Col 9, Line 6, Col 13, """Invalid warning number '"FS"'""");
(Warning 203, Line 7, Col 9, Line 7, Col 17, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 8, Col 9, Line 8, Col 15, """Invalid warning number '"ACME"'""")
(Warning 203, Line 6, Col 9, Line 6, Col 13, "Invalid warning number 'FS'");
(Warning 203, Line 7, Col 9, Line 7, Col 17, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 8, Col 9, Line 8, Col 15, "Invalid warning number 'ACME'")
]


Expand Down Expand Up @@ -65,15 +65,15 @@ module NonStringArgs =
(Error 3350, Line 4, Col 5, Line 4, Col 7, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 5, Col 5, Line 5, Col 11, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 6, Col 5, Line 6, Col 9, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Warning 203, Line 7, Col 5, Line 7, Col 9, """Invalid warning number '"FS"'""");
(Warning 203, Line 8, Col 5, Line 8, Col 13, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 7, Col 5, Line 7, Col 9, "Invalid warning number 'FS'");
(Warning 203, Line 8, Col 5, Line 8, Col 13, "Invalid warning number 'FSBLAH'");
else
(Warning 203, Line 4, Col 5, Line 4, Col 7, "Invalid warning number 'FS'");
(Warning 203, Line 5, Col 5, Line 5, Col 11, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 6, Col 5, Line 6, Col 9, "Invalid warning number 'ACME'");
(Warning 203, Line 7, Col 5, Line 7, Col 9, """Invalid warning number '"FS"'""");
(Warning 203, Line 8, Col 5, Line 8, Col 13, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 9, Col 5, Line 9, Col 11, """Invalid warning number '"ACME"'""")
(Warning 203, Line 7, Col 5, Line 7, Col 9, "Invalid warning number 'FS'");
(Warning 203, Line 8, Col 5, Line 8, Col 13, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 9, Col 5, Line 9, Col 11, "Invalid warning number 'ACME'")
]


Expand All @@ -95,15 +95,15 @@ module NonStringArgs =
(Error 3350, Line 3, Col 9, Line 3, Col 11, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 3, Col 12, Line 3, Col 18, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 3, Col 19, Line 3, Col 23, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Warning 203, Line 3, Col 24, Line 3, Col 28, """Invalid warning number '"FS"'""");
(Warning 203, Line 3, Col 29, Line 3, Col 37, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 3, Col 24, Line 3, Col 28, "Invalid warning number 'FS'");
(Warning 203, Line 3, Col 29, Line 3, Col 37, "Invalid warning number 'FSBLAH'");
else
(Warning 203, Line 3, Col 9, Line 3, Col 11, "Invalid warning number 'FS'");
(Warning 203, Line 3, Col 12, Line 3, Col 18, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 3, Col 19, Line 3, Col 23, "Invalid warning number 'ACME'");
(Warning 203, Line 3, Col 24, Line 3, Col 28, """Invalid warning number '"FS"'""");
(Warning 203, Line 3, Col 29, Line 3, Col 37, """Invalid warning number '"FSBLAH"'""");
(Warning 203, Line 3, Col 38, Line 3, Col 44, """Invalid warning number '"ACME"'""")
(Warning 203, Line 3, Col 24, Line 3, Col 28, "Invalid warning number 'FS'");
(Warning 203, Line 3, Col 29, Line 3, Col 37, "Invalid warning number 'FSBLAH'");
(Warning 203, Line 3, Col 38, Line 3, Col 44, "Invalid warning number 'ACME'")
]


Expand Down Expand Up @@ -145,7 +145,7 @@ module DoBinding =
(Warning 1104, Line 5, Col 15, Line 5, Col 31, "Identifiers containing '@' are reserved for use in F# code generation")
(Error 3350, Line 2, Col 9, Line 2, Col 11, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Error 3350, Line 2, Col 12, Line 2, Col 18, "Feature '# directives with non-quoted string arguments' is not available in F# 8.0. Please use language version 9.0 or greater.")
(Warning 203, Line 2, Col 26, Line 2, Col 34, """Invalid warning number '"FS3221"'""")
(Warning 203, Line 2, Col 26, Line 2, Col 34, "Invalid warning number 'FS3221'")
]
else
compileResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ImplFile
(ParsedImplFileInput
("/root/ParsedHashDirective/TripleQuoteStringAsParsedHashDirectiveArgument.fs",
false, QualifiedNameOfFile TripleQuoteStringAsParsedHashDirectiveArgument,
[WarningOff ((2,0--2,16), 40)], [],
[WarningOff ((2,8--2,16), 40)], [],
[SynModuleOrNamespace
([TripleQuoteStringAsParsedHashDirectiveArgument], false, AnonModule,
[HashDirective
Expand Down

0 comments on commit b3a1288

Please sign in to comment.