-
-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate expressive diagnostic messages with the offending line #8487
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
dmd now supports expressive diagnostic error messages with `-verrors=context` | ||
|
||
With the new CLI option `-verrors=context` dmd will now show the offending line directly in its error messages. | ||
Consider this faulty program `test.d`: | ||
|
||
--- | ||
void foo() | ||
{ | ||
a = 1; | ||
} | ||
--- | ||
|
||
Now run it with `-verrors=context`: | ||
|
||
$(CONSOLE | ||
> dmd -verrors=context test.d | ||
test.d(4): $(RED Error): undefined identifier a | ||
a = 1; | ||
^ | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* Compiler implementation of the | ||
* $(LINK2 http://www.dlang.org, D programming language). | ||
* | ||
* Copyright: Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved | ||
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright) | ||
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) | ||
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/filecache.d, filecache.d) | ||
* Documentation: https://dlang.org/phobos/dmd_filecache.html | ||
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/filecache.d | ||
*/ | ||
|
||
module dmd.filecache; | ||
|
||
import dmd.root.stringtable; | ||
import dmd.root.array; | ||
import dmd.root.file; | ||
|
||
import core.stdc.stdio; | ||
|
||
/** | ||
A line-by-line representation of a $(REF File, dmd,root,file). | ||
*/ | ||
class FileAndLines | ||
{ | ||
File* file; | ||
const(char[])[] lines; | ||
|
||
/** | ||
File to read and split into its lines. | ||
*/ | ||
this(const(char)[] filename) | ||
{ | ||
file = new File(filename); | ||
readAndSplit(); | ||
} | ||
|
||
// Read a file and split the file buffer linewise | ||
private void readAndSplit() | ||
{ | ||
file.read(); | ||
auto buf = file.buffer; | ||
// slice into lines | ||
while (*buf) | ||
{ | ||
auto prevBuf = buf; | ||
for (; *buf != '\n' && *buf != '\r'; buf++) | ||
{ | ||
if (!*buf) | ||
break; | ||
} | ||
// handle Windows line endings | ||
if (*buf == '\r' && *(buf + 1) == '\n') | ||
buf++; | ||
lines ~= cast(const(char)[]) prevBuf[0 .. buf - prevBuf]; | ||
buf++; | ||
} | ||
} | ||
|
||
void destroy() | ||
{ | ||
if (file) | ||
{ | ||
file.destroy(); | ||
file = null; | ||
lines.destroy(); | ||
lines = null; | ||
} | ||
} | ||
|
||
~this() | ||
{ | ||
destroy(); | ||
} | ||
} | ||
|
||
/** | ||
A simple file cache that can be used to avoid reading the same file multiple times. | ||
It stores its cached files as $(LREF FileAndLines) | ||
*/ | ||
struct FileCache | ||
{ | ||
private StringTable files; | ||
|
||
/** | ||
Add or get a file from the file cache. | ||
If the file isn't part of the cache, it will be read from the filesystem. | ||
If the file has been read before, the cached file object will be returned | ||
|
||
Params: | ||
file = file to load in (or get from) the cache | ||
|
||
Returns: a $(LREF FileAndLines) object containing a line-by-line representation of the requested file | ||
*/ | ||
FileAndLines addOrGetFile(const(char)[] file) | ||
{ | ||
if (auto payload = files.lookup(file)) | ||
{ | ||
if (payload !is null) | ||
return cast(typeof(return)) payload.ptrvalue; | ||
} | ||
|
||
auto lines = new FileAndLines(file); | ||
files.insert(file, cast(void*) lines); | ||
return lines; | ||
} | ||
|
||
__gshared fileCache = FileCache(); | ||
|
||
// Initializes the global FileCache singleton | ||
static __gshared void _init() | ||
{ | ||
fileCache.initialize(); | ||
} | ||
|
||
void initialize() | ||
{ | ||
files._init(); | ||
} | ||
|
||
void deinitialize() | ||
{ | ||
foreach (sv; files) | ||
sv.destroy(); | ||
files.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
REQUIRED_ARGS: -verrors=context | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/fail_pretty_errors.d(20): Error: undefined identifier `a` | ||
a = 1; | ||
^ | ||
fail_compilation/fail_pretty_errors.d-mixin-25(25): Error: undefined identifier `b` | ||
fail_compilation/fail_pretty_errors.d(30): Error: cannot implicitly convert expression `5` of type `int` to `string` | ||
string x = 5; | ||
^ | ||
fail_compilation/fail_pretty_errors.d(35): Error: mixin `fail_pretty_errors.testMixin2.mixinTemplate!()` error instantiating | ||
mixin mixinTemplate; | ||
^ | ||
--- | ||
*/ | ||
|
||
void foo() | ||
{ | ||
a = 1; | ||
} | ||
wilzbach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void testMixin1() | ||
{ | ||
mixin("b = 1;"); | ||
} | ||
|
||
mixin template mixinTemplate() | ||
{ | ||
string x = 5; | ||
} | ||
|
||
void testMixin2() | ||
{ | ||
mixin mixinTemplate; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the addition of
-mixin=<filename>
might want to doublecheck that logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. For now, I just added
&& !global.params.mixinOut
as that's definitely safe.In the future, we probably need to call
flushMixins
or make the fileCache aware of mixin files.