Flutter Engine
The Flutter Engine
report.cc
Go to the documentation of this file.
1// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/report.h"
6
7#include "vm/code_patcher.h"
8#include "vm/exceptions.h"
9#include "vm/flags.h"
10#include "vm/longjump.h"
11#include "vm/object.h"
12#include "vm/stack_frame.h"
13#include "vm/symbols.h"
14
15namespace dart {
16
17DEFINE_FLAG(bool, silent_warnings, false, "Silence warnings.");
18DEFINE_FLAG(bool, warning_as_error, false, "Treat warnings as errors.");
19
21 const Script& script,
22 TokenPosition token_pos,
23 bool report_after_token,
24 const String& message) {
25 const char* message_header;
26 switch (kind) {
27 case kWarning:
28 message_header = "warning";
29 break;
30 case kError:
31 message_header = "error";
32 break;
33 case kBailout:
34 message_header = "bailout";
35 break;
36 default:
37 message_header = "";
39 }
41 if (!script.IsNull() && script.HasSource()) {
42 const String& script_url = String::Handle(script.url());
43 intptr_t line, column;
44 if (script.GetTokenLocation(token_pos, &line, &column)) {
45 const intptr_t token_len = script.GetTokenLength(token_pos);
46 if (report_after_token) {
47 column += token_len < 0 ? 1 : token_len;
48 }
49 // Allocate formatted strings in old space as they may be created during
50 // optimizing compilation. Those strings are created rarely and should not
51 // pollute old space.
53 Heap::kOld, "'%s': %s: line %" Pd " pos %" Pd ": ",
54 script_url.ToCString(), message_header, line, column);
55 // Append the formatted error or warning message.
56 const Array& strs = Array::Handle(Array::New(6, Heap::kOld));
57 strs.SetAt(0, result);
58 strs.SetAt(1, message);
59 // Append the source line.
60 const String& script_line =
62 ASSERT(!script_line.IsNull());
63 strs.SetAt(2, Symbols::NewLine());
64 strs.SetAt(3, script_line);
65 strs.SetAt(4, Symbols::NewLine());
66 // Append the column marker.
67 const String& column_line = String::Handle(String::NewFormatted(
68 Heap::kOld, "%*s\n", static_cast<int>(column), "^"));
69 strs.SetAt(5, column_line);
71 } else {
72 // Token position is unknown.
74 Heap::kOld, "'%s': %s: ", script_url.ToCString(), message_header);
76 }
77 } else {
78 // Script is unknown.
79 // Append the formatted error or warning message.
80 result = String::NewFormatted(Heap::kOld, "%s: ", message_header);
82 }
83 return result.ptr();
84}
85
89}
90
91void Report::LongJumpF(const Error& prev_error,
92 const Script& script,
93 TokenPosition token_pos,
94 const char* format,
95 ...) {
96 va_list args;
98 LongJumpV(prev_error, script, token_pos, format, args);
99 va_end(args);
100 UNREACHABLE();
101}
102
103void Report::LongJumpV(const Error& prev_error,
104 const Script& script,
105 TokenPosition token_pos,
106 const char* format,
107 va_list args) {
108 // If an isolate is being killed a [UnwindError] will be propagated up the
109 // stack. In such a case we cannot wrap the unwind error in a new
110 // [LanguageError]. Instead we simply continue propagating the [UnwindError]
111 // upwards.
112 if (prev_error.IsUnwindError()) {
113 LongJump(prev_error);
114 UNREACHABLE();
115 }
117 prev_error, script, token_pos, Report::AtLocation, kError, Heap::kOld,
118 format, args));
120 UNREACHABLE();
121}
122
124 const Script& script,
125 TokenPosition token_pos,
126 bool report_after_token,
127 const char* format,
128 ...) {
129 va_list args;
131 MessageV(kind, script, token_pos, report_after_token, format, args);
132 va_end(args);
133}
134
136 const Script& script,
137 TokenPosition token_pos,
138 bool report_after_token,
139 const char* format,
140 va_list args) {
141 if (kind < kError) {
142 // Reporting a warning.
143 if (FLAG_silent_warnings) {
144 return;
145 }
146 if (!FLAG_warning_as_error) {
148 const String& snippet_msg = String::Handle(
149 PrependSnippet(kind, script, token_pos, report_after_token, msg));
150 OS::PrintErr("%s", snippet_msg.ToCString());
151 return;
152 }
153 }
154 // Reporting an error (or a warning as error).
156 Error::Handle(), // No previous error.
157 script, token_pos, report_after_token, kind, Heap::kOld, format, args));
159 UNREACHABLE();
160}
161
162} // namespace dart
#define UNREACHABLE()
Definition: assert.h:248
static ArrayPtr New(intptr_t len, Heap::Space space=Heap::kNew)
Definition: object.h:10959
void SetAt(intptr_t index, const Object &value) const
Definition: object.h:10880
@ kOld
Definition: heap.h:39
static LanguageErrorPtr static LanguageErrorPtr NewFormattedV(const Error &prev_error, const Script &script, TokenPosition token_pos, bool report_after_token, Report::Kind kind, Heap::Space space, const char *format, va_list args)
Definition: object.cc:19834
DART_NORETURN void Jump(int value, const Error &error)
Definition: longjump.cc:22
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
bool IsNull() const
Definition: object.h:363
static Object & Handle()
Definition: object.h:407
static void static void MessageV(Kind kind, const Script &script, TokenPosition token_pos, bool report_after_token, const char *format, va_list args)
Definition: report.cc:135
static DART_NORETURN void LongJump(const Error &error)
Definition: report.cc:86
static DART_NORETURN void LongJumpF(const Error &prev_error, const Script &script, TokenPosition token_pos, const char *format,...) PRINTF_ATTRIBUTE(4
Definition: report.cc:91
static void MessageF(Kind kind, const Script &script, TokenPosition token_pos, bool report_after_token, const char *format,...) PRINTF_ATTRIBUTE(5
Definition: report.cc:123
static DART_NORETURN void static DART_NORETURN void LongJumpV(const Error &prev_error, const Script &script, TokenPosition token_pos, const char *format, va_list args)
Definition: report.cc:103
@ kBailout
Definition: report.h:26
@ kWarning
Definition: report.h:24
static constexpr bool AtLocation
Definition: report.h:29
static StringPtr PrependSnippet(Kind kind, const Script &script, TokenPosition token_pos, bool report_after_token, const String &message)
Definition: report.cc:20
static StringPtr NewFormatted(const char *format,...) PRINTF_ATTRIBUTE(1
Definition: object.cc:24004
static StringPtr static StringPtr static StringPtr NewFormattedV(const char *format, va_list args, Heap::Space space=Heap::kNew)
Definition: object.cc:24022
static StringPtr ConcatAll(const Array &strings, Heap::Space space=Heap::kNew)
Definition: object.cc:24048
static StringPtr Concat(const String &str1, const String &str2, Heap::Space space=Heap::kNew)
Definition: object.cc:24037
static const char * ToCString(Thread *thread, StringPtr ptr)
Definition: object.cc:24126
static const String & NewLine()
Definition: symbols.h:651
LongJumpScope * long_jump_base() const
Definition: thread_state.h:47
static Thread * Current()
Definition: thread.h:362
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
uint32_t uint32_t * format
Win32Message message
va_start(args, format)
va_end(args)
Definition: dart_vm.cc:33
DEFINE_FLAG(bool, print_cluster_information, false, "Print information about clusters written to snapshot")
#define Pd
Definition: globals.h:408