Commit Graph

128 Commits

Author SHA1 Message Date
Henrik G. Olsson
da55801862 [DiagnosticVerifier] Add -verify-ignore-macro-note
The _SwiftifyImport macro is emitted into an unnamed buffer and then
parsed, pretending it was in the header all along. This makes it hard to
add `expected-note` comments for `diag::in_macro_expansion` when they
point here. That's okay, because the macro expansion has already been
pointed out by `expected-expansion` directives. But
-verify-ignore-unrelated is too blunt of a tool, so this adds
-verify-ignore-macro-note to ignore these specific diagnostics.
2025-10-13 18:35:46 -07:00
Henrik G. Olsson
f343289c45 [DiagnosticVerifier] Fix Twine use after free
`llvm::Twine` is not valid after the end of the statement. Pass it
directly as a parameter without storing it in a local variable to
prevent UAF. Also updates DiagnosticVerifier tests to capture all
relevant output.
2025-10-07 20:19:14 -07:00
Henrik G. Olsson
b2ddd689e4 [DiagnosticVerifier] Enable -verify-ignore-unrelated
This enables the previously added -verify-ignore-unrelated flag. When
-verify is used without -verify-ignore-unrelated, diagnostics emitted in
buffers other than the main file and those passed with
-verify-additional-file (except diagnostics emitted at <unknown>:0) will
now result in an error. They were previously ignored. The old behaviour
is still available as opt-in using -verify-ignore-unrelated.
2025-10-04 14:20:01 -07:00
Henrik G. Olsson
e0c65b7c44 [DiagnosticVerifier] Add -verify-ignore-unrelated flag
This adds the implementation required for later changing the default
behaviour of the -verify flag to error when diagnostics are emitted
in buffers other than the main file and files added with
-verify-additional-file. To keep the current behaviour, use the flag
-verify-ignore-unrelated. This flag is added as a no-op so that tests
can start using it before the new behaviour is enabled by default.
2025-10-04 12:40:59 -07:00
John Hui
e97fa2430b [DiagnosticVerifier] Do not capture 'fatal error encountered' errors (#84640)
When the diagnostic verifier encounters a fatal error, it reports:

    <unknown>:0: error: fatal error encountered while in -verify mode

If we capture this, and fail to match this against an expected-error,
the diagnostic verifier complains:

    <unknown>:0: error: unexpected error produced: fatal error encountered while in -verify mode
    <unknown>:0: error: diagnostic produced elsewhere: fatal error encountered while in -verify mode

The current workaround is to use -verify-ignore-unknown, but that in
turn may mask actual errors coming from unknown source locations.
Ignoring these errors about errors removes the need for that workaround.
2025-10-02 11:14:59 -07:00
Henrik G. Olsson
c5256ff341 Fix continue statements incorrectly changed to returns
When this function was extracted from a for loop, continue was mapped to
return, but these nested continues should remain.
2025-09-19 14:20:04 -07:00
Henrik G. Olsson
5422c8d284 Fix bug where column is specified for own line
When the syntax `expected-error@:42{{}}` was used, this would
accidentally trigger "absolute line" mode, despite not specifying a
line, resulting in the target line always being line 0.
2025-09-19 12:43:07 -07:00
Henrik G. Olsson
e3d92dbf0f [DiagnosticVerifier] implement expected-expansion
Since freestanding macro expansion buffer names include the line number
of their invocation, it can become quite fiddly to try to update a test
file with multiple macro expansions. This adds the option to use an
expected-expansion block and use a relative line number, nesting other
expected diagnostic statements inside this block.

Example syntax:
```swift
let myVar = #myMacro
/*
expected-expansion@-2:1{{
  expected-error@13:37{{I can't believe you've done this}}
  expected-note@14:38{{look at this and ponder your mistake}}
}}
*/
```
2025-09-18 16:33:42 -07:00
Henrik G. Olsson
da56a52fbd [DiagnosticVerifier] Extract parseExpectedDiagInfo (NFCI)
This refactors the parsing of "expected-*" lines to a separate function,
to enable recursive parsing in the next commit.
2025-09-17 12:48:18 -07:00
Henrik G. Olsson
687449bfb1 [Frontend] Check diagnostics in generated sources
The previous commit added support for checking diagnostics in other
buffers, enabling diagnostic verification for e.g. macro expansions.
Because the diagnostics are in a different buffer, there is no error for
not marking them as "expected". This commit expands the scope to
include generated sources originating in code in any checked buffer, to
help make sure that errors aren't accidentally missed.
2025-09-15 14:27:51 -07:00
Henrik G. Olsson
e3d4870dc4 [Frontend] Add support for named target buffer in -verify expected lines
Clang, which heavily inspired Swift's -verify flag, supports naming
other files like so:
```
// expected-error@some-header.h:11:22{{some error}}
```
Swift hasn't had the same need for this, because of the lack of textual
header inclusion (where the same header file can emit different
diagnostics depending on where it's included). The lack of this
functionality has made it impossible to use -verify in cases where
diagnostics are emitted in a macro however, since the expected-lines
can't be placed inside the generated macro buffer.

Now the main Swift file can refer to diagnostics inside these generated
buffers by naming the buffers. The generated names aren't pretty, but
the identifier is stable, unique and it works.

Here is an example of what it can look like:
```
// expected-error@@__swiftmacro_4main3bar.swift:10:15{{no exact matches in call to initializer}}
// expected-note@+1{{in expansion of macro 'foo' on global function 'bar' here}}
@foo
func bar() {}
```
The double "@" is a result of the buffer name starting with an @, which
is unfortunate but is how the mangling scheme works for macro buffer
names.
2025-09-15 14:27:47 -07:00
Anthony Latsis
06a5670c8f Basic: Untie swift::SourceLoc from llvm::SMLoc
Storing a `llvm::SMLoc` is a superfluous indirection, and getting rid of
it enables us to unconditionally import `SourceLoc` into Swift.
2025-07-11 18:48:42 +01:00
Doug Gregor
e88f8995e1 [Diagnostics] Eliminate educational notes in favor of diagnostic groups
We've been converging the implementations of educational notes and
diagnostic groups, where both provide category information in
diagnostics (e.g., `[#StrictMemorySafety]`) and corresponding
short-form documentation files. The diagnostic group model is more
useful in a few ways:

* It provides warnings-as-errors control for warnings in the group
* It is easier to associate a diagnostic with a group with
GROUPED_ERROR/GROUPED_WARNING than it is to have a separate diagnostic
ID -> mapping.
* It is easier to see our progress on diagnostic-group coverage
* It provides an easy name to use for diagnostic purposes.

Collapse the educational-notes infrastructure into diagnostic groups,
migrating all of the existing educational notes into new groups.
Simplify the code paths that dealt with multiple educational notes to
have a single, possibly-missing "category documentation URL", which is
how we're treating this.
2025-03-29 15:40:35 -07:00
Ryan Mansfield
7c97328569 Fix -Wreorder-ctor warning in DiagnosticVerifier.cpp. 2025-02-18 16:24:28 -05:00
Becca Royal-Gordon
0466d5c0ca Handle diagnostic verifier locations concretely
This commit makes a number of adjustments to how the diagnostic verifier handles source buffers and source locations. Specifically:

• Files named by `-verify-additional-file` are read as late as possible so that if some other component of the compiler has already loaded the file, even in some exotic way (e.g. ClangImporter’s source buffer mirroring), it will use the same buffer.
• Expectation source locations now ignore virtual files and other trickery; they are based on the source buffer and physical location in the file.

Hopefully this will make `-verify-additional-file` work better on Windows. As an unintended side effect, it also changes how expectations work in tests that use `#sourceLocation()`.
2025-02-11 12:05:17 -08:00
Ben Barham
d72f5b12c4 Update StringRef::equals references to operator==
`equals` has been deprecated upstream, use `operator==` instead.
2024-06-27 19:14:06 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Ben Barham
9779c18da3 Rename startswith to starts_with
LLVM is presumably moving towards `std::string_view` -
`StringRef::startswith` is deprecated on tip. `SmallString::startswith`
was just renamed there (maybe with some small deprecation inbetween, but
if so, we've missed it).

The `SmallString::startswith` references were moved to
`.str().starts_with()`, rather than adding the `starts_with` on
`stable/20230725` as we only had a few of them. Open to switching that
over if anyone feels strongly though.
2024-03-13 22:25:47 -07:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Hamish Knight
b61ee24d1b [Frontend] Support color output in DiagnosticVerifier
Move the color stream utilities into ColorUtils.h,
and use ColorStream to print diagnostic messages
if `-color-diagnostics` is passed.
2024-02-09 16:47:03 +00:00
Michael Gottesman
fa1558a175 [frontend] Add an option called -verify-additional-prefix to add additional check prefixes to the DiagnosticVerifier.
This enables one to use varying prefixes when checking diagnostics with the
DiagnosticVerifier. So for instance, I can make a test work both with and
without send-non-sendable enabled by adding additional prefixes. As an example:

```swift
// RUN: %target-swift-frontend ... -verify-additional-prefix no-sns-
// RUN: %target-swift-frontend ... -verify-additional-prefix sns-

let x = ... // expected-error {{This is always checked no matter what prefixes I added}}
let y = ... // expected-no-sns-error {{This is only checked if send non sendable is disabled}}
let z = ... // expected-sns-error {{This is only checked if send non sendable is enabled}}
let w = ... // expected-no-sns-error {{This is checked for a specific error when sns is disabled...}}
// expected-sns-error @-1 {{and for a different error when sns is enabled}}
```

rdar://114643840
2023-08-30 13:40:17 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Anthony Latsis
3998fdf0c4 DiagnosticVerifier: Optimize CapturedFixItInfo::getLineColumnRange for intra-line source ranges 2023-03-08 13:18:22 +03:00
Anthony Latsis
d2911437c0 DiagnosticVerifier: Use zeros instead of LineColumnRange::NoValue 2023-03-08 13:18:16 +03:00
Anthony Latsis
14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Anthony Latsis
8306f870ec DiagnosticVerifier: Default expected fix-it end line to start line 2023-03-08 12:10:27 +03:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Becca Royal-Gordon
cdcd726f92 Add fixit alternation to -verify
You can now put `||` between two fix-its to indicate that the test succeeds if either of them is present. This is meant for situations where a fix-it might vary slightly in different subtests or test configurations.

Also fixes a bug in the diagnostic verifier where "expected-whatever" would search beyond the same line for its opening "{{", potentially finding one many lines away and giving a bad diagnostic and poor recovery behavior.
2022-03-04 10:28:42 -08:00
swift-ci
ad903094c0 Merge pull request #41386 from beccadax/your-library-is-overdue
Weaken some type checks for @preconcurrency decls
2022-03-01 16:13:59 -08:00
Rintaro Ishizaki
1ee91db867 [DiagnosticVerifier] Define LineColumnRange::NoValue 2022-02-19 16:53:57 -08:00
Becca Royal-Gordon
5330653f3d Improve -verify’s wrong diagnostic kind diagnosis
If, for instance, an error is emitted as a warning instead, the verifier now detects this and emits a single diagnostic saying that the warning was found but had the wrong kind, instead of emitting one diagnostic saying the error was missing and another saying the warning was unexpected.

In theory there are some edge cases we could handle better by doing two separate passes—one to detect exact expectation matches and remove them, another to detect near-misses and diagnose them—but in practice, I think the text + diagnostic location is likely to be unique enough to keep this from being a problem. (I would hesitate to do wrong-line diagnostics in the same pass like this, though.)
2022-02-18 13:27:59 -08:00
Anthony Latsis
2128678d56 DiagnosticVerifier: Support line offsets in fix-it verification ranges 2022-02-18 04:42:57 +03:00
Anthony Latsis
4ebd9c0131 [NFC] DiagnosticVerifier: Remove redundant variable 2022-01-31 05:04:49 +03:00
Anthony Latsis
91fedd9b8a Verifier: Support line numbers in fix-it verification 2022-01-31 05:04:49 +03:00
Anthony Latsis
d5087ee329 [NFC] DiagnosticVerifier: Cache computed column numbers for actual fix-its 2022-01-31 05:04:33 +03:00
Anthony Latsis
011ce73e0c DiagnosticVerifier: Fix no-op range assignment in DiagnosticVerifier::handleDiagnostic 2022-01-31 05:04:33 +03:00
Anthony Latsis
99b8ae09d0 [NFC] DiagnosticVerifier: Use a dedicated struct for fix-it column-based ranges 2022-01-31 05:04:22 +03:00
Anthony Latsis
8e4ea9b01a [NFC] DiagnosticVerifier: Move 'getColumnNumber' into SourceManager 2022-01-31 05:04:09 +03:00
Evan Wilde
0aafd09835 F_None was renamed OF_None
This patch updates usages of F_None to OF_None, as LLVM changed that in
commit 3302af9d4c39642bebe64dd60a3aa162fefc44b2.
2021-06-23 10:36:39 -07:00
Becca Royal-Gordon
1f713074ac [NFC] Fix issues with -verify mode
This commit fixes two weird bugs in -verify mode:

1. SourceLocs from the wrong SourceManager could be passed through a ForwardingDiagnosticConsumer into the DiagnosticVerifier.

2. -verify-additional-file did not error out correctly when the file couldn’t be opened.

No tests, as we only have basic tests for the diagnostic verifier.
2021-03-25 16:02:29 -07:00
Brent Royal-Gordon
5036a55550 [Frontend] Allow additional files for diagnostic verifier
This change adds a frontend flag, -verify-additional-file, which can be used to pass extra files directly to the diagnostic verifier. These files are not otherwise considered to be Swift source files; they are not compiled or even properly parsed.

This feature can be used to verify diagnostics emitted in non-source files, such as in module interfaces or header files.
2021-01-11 15:59:25 -08:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
Owen Voorhees
45bc578ae5 [SourceManager] Rename line and column APIs for clarity 2020-05-21 12:54:07 -05:00
Matt Davis
feb03ebcd7 [DiagnosticVerifier] Explicit convert a StringRef into a std::string.
The newer llvm StringRef library has removed the implicit StringRef to
std::string conversion.  (See: llvm/llvm-project@adcd026)

This patch also uses a StringRef to compare another StringRef
eliminating the need to perform a StringRef to std::string conversion.
I am concerned that StringRef's are being stored in
ExpectedEducationalNotes, but as long as the StringRef does not out live
the definition this is totally cool then.
2020-04-11 09:29:58 -07:00
Owen Voorhees
456081715e Merge pull request #30947 from owenv/verifier-asan-fix
[DiagnosticVerifier] Fix ASAN issue in the verifier
2020-04-10 11:25:51 -05:00
Owen Voorhees
20d3afc439 [DiagnosticVerifier] Fix ASAN issue where Twine was stored to a temporary 2020-04-10 04:53:05 -07:00
Owen Voorhees
cfbedd81c4 [DiagnosticVerifier] Add support for asserting presence of edu notes 2020-04-09 18:18:25 -07:00
omochimetaru
a07b35ab03 [Diagnostics] Improve {{none}} fix-it verifier (#30791)
* [Diagnostics] Improve {{none}} fix-it verifier

* split two conditions

* define "none" constant

* support plural

* use Twine and add comment for replacement range

* check if {{none}} is at the end

* use noneMarkerStartLoc

* update second {{none}} error message

Co-Authored-By: Owen Voorhees <owenvoorhees@gmail.com>

* update test case for second {{none}}

* fix test case for new {{none}} check

* Use named struct

* set const

Co-authored-by: Owen Voorhees <owenvoorhees@gmail.com>
2020-04-09 15:32:14 -07:00
Owen Voorhees
d6a2910f41 Update DiagnosticVerifier.cpp 2020-04-01 15:12:30 -07:00
Owen Voorhees
2c2850d7eb Merge branch 'master' into verifier-wording-fix 2020-04-01 17:05:24 -05:00