summaryrefslogtreecommitdiffstats
path: root/source/slang/diagnostics.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoley@nvidia.com>2017-08-09 12:57:37 -0700
committerTim Foley <tfoley@nvidia.com>2017-08-10 13:05:04 -0700
commita5a436c4783fb75a0d089a6483219c06db91f593 (patch)
tree224c16ad374c5ed533a497beeb75753e7ce2d771 /source/slang/diagnostics.cpp
parent6e4830f4d74adef0a47c6503d84dc114240fafa3 (diff)
Make source location lightweight
Fixes #24 So far the code has used a representation for source locations that is heavy-weight, but typical of research or hobby compilers: a `struct` type containing a line number and a (heap-allocated) string. This is actually very convenient for debugging, but it means that any data structure that might contain a source location needs careful memory management (because of those strings) and has a tendency to bloat. The new represnetation is that a source location is just a pointer-sized integer. In the simplest mental model, you can think of this as just counting every byte of source text that is passed in, and using those to name locations. Finding the path and line number that corresponds to a location involves a lookup step, but we can arrange to store all the files in an array sorted by their start locations, and do a binary search. Finding line numbers inside a file is similarly fast (one you pay a one-time cost to build an array of starting offsets for lines). More advanced compilers like clang actually go further and create a unique range of source locations to represent a file each time it gets included, so that they can track the include stack and reproduce it in diagnostic messages. I'm not doing anything that clever here.
Diffstat (limited to 'source/slang/diagnostics.cpp')
-rw-r--r--source/slang/diagnostics.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/source/slang/diagnostics.cpp b/source/slang/diagnostics.cpp
index e0b959a81..eb6a818d3 100644
--- a/source/slang/diagnostics.cpp
+++ b/source/slang/diagnostics.cpp
@@ -1,7 +1,8 @@
-// Diagnostics.cpp
-#include "Diagnostics.h"
+// diagnostics.cpp
+#include "diagnostics.h"
-#include "Syntax.h"
+#include "compiler.h"
+#include "syntax.h"
#include <assert.h>
@@ -67,17 +68,17 @@ void printDiagnosticArg(StringBuilder& sb, Token const& token)
sb << token.Content;
}
-CodePosition const& getDiagnosticPos(SyntaxNode const* syntax)
+SourceLoc const& getDiagnosticPos(SyntaxNode const* syntax)
{
return syntax->Position;
}
-CodePosition const& getDiagnosticPos(Token const& token)
+SourceLoc const& getDiagnosticPos(Token const& token)
{
return token.Position;
}
-CodePosition const& getDiagnosticPos(TypeExp const& typeExp)
+SourceLoc const& getDiagnosticPos(TypeExp const& typeExp)
{
return typeExp.exp->Position;
}
@@ -140,12 +141,18 @@ static void formatDiagnosticMessage(StringBuilder& sb, char const* format, int a
}
static void formatDiagnostic(
+ DiagnosticSink* sink,
StringBuilder& sb,
Diagnostic const& diagnostic)
{
- sb << diagnostic.Position.FileName;
+ auto sourceManager = sink->sourceManager;
+
+ auto expandedLoc = sourceManager->expandSourceLoc(diagnostic.Position);
+ auto humaneLoc = sourceManager->getHumaneLoc(expandedLoc);
+
+ sb << humaneLoc.getPath();
sb << "(";
- sb << diagnostic.Position.Line;
+ sb << humaneLoc.line;
sb << "): ";
sb << getSeverityName(diagnostic.severity);
sb << " ";
@@ -155,7 +162,7 @@ static void formatDiagnostic(
sb << "\n";
}
-void DiagnosticSink::diagnoseImpl(CodePosition const& pos, DiagnosticInfo const& info, int argCount, DiagnosticArg const* const* args)
+void DiagnosticSink::diagnoseImpl(SourceLoc const& pos, DiagnosticInfo const& info, int argCount, DiagnosticArg const* const* args)
{
StringBuilder sb;
formatDiagnosticMessage(sb, info.messageFormat, argCount, args);
@@ -176,7 +183,7 @@ void DiagnosticSink::diagnoseImpl(CodePosition const& pos, DiagnosticInfo const&
{
// If so, pass the error string along to them
StringBuilder messageBuilder;
- formatDiagnostic(messageBuilder, diagnostic);
+ formatDiagnostic(this, messageBuilder, diagnostic);
callback(messageBuilder.ProduceString().begin(), callbackUserData);
}
@@ -184,7 +191,7 @@ void DiagnosticSink::diagnoseImpl(CodePosition const& pos, DiagnosticInfo const&
{
// If the user doesn't have a callback, then just
// collect our diagnostic messages into a buffer
- formatDiagnostic(outputBuffer, diagnostic);
+ formatDiagnostic(this, outputBuffer, diagnostic);
}
if (diagnostic.severity >= Severity::Fatal)