Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(914)

Side by Side Diff: runtime/vm/parser.cc

Issue 68113028: Lazily format LanguageError messages (fix issue 15069). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "lib/invocation_mirror.h" 7 #include "lib/invocation_mirror.h"
8 #include "platform/utils.h" 8 #include "platform/utils.h"
9 #include "vm/bootstrap.h" 9 #include "vm/bootstrap.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
(...skipping 5210 matching lines...) Expand 10 before | Expand all | Expand 10 after
5221 // with the formal parameter types and names. 5221 // with the formal parameter types and names.
5222 void Parser::AddFormalParamsToFunction(const ParamList* params, 5222 void Parser::AddFormalParamsToFunction(const ParamList* params,
5223 const Function& func) { 5223 const Function& func) {
5224 ASSERT((params != NULL) && (params->parameters != NULL)); 5224 ASSERT((params != NULL) && (params->parameters != NULL));
5225 ASSERT((params->num_optional_parameters > 0) == 5225 ASSERT((params->num_optional_parameters > 0) ==
5226 (params->has_optional_positional_parameters || 5226 (params->has_optional_positional_parameters ||
5227 params->has_optional_named_parameters)); 5227 params->has_optional_named_parameters));
5228 if (!Utils::IsInt(16, params->num_fixed_parameters) || 5228 if (!Utils::IsInt(16, params->num_fixed_parameters) ||
5229 !Utils::IsInt(16, params->num_optional_parameters)) { 5229 !Utils::IsInt(16, params->num_optional_parameters)) {
5230 const Script& script = Script::Handle(Class::Handle(func.Owner()).script()); 5230 const Script& script = Script::Handle(Class::Handle(func.Owner()).script());
5231 const Error& error = Error::Handle(FormatErrorMsg( 5231 const Error& error = Error::Handle(LanguageError::NewFormatted(
5232 script, func.token_pos(), "Error", "too many formal parameters")); 5232 Error::Handle(), script, func.token_pos(),
5233 LanguageError::kError, Heap::kNew,
5234 "too many formal parameters"));
5233 ErrorMsg(error); 5235 ErrorMsg(error);
5234 } 5236 }
5235 func.set_num_fixed_parameters(params->num_fixed_parameters); 5237 func.set_num_fixed_parameters(params->num_fixed_parameters);
5236 func.SetNumOptionalParameters(params->num_optional_parameters, 5238 func.SetNumOptionalParameters(params->num_optional_parameters,
5237 params->has_optional_positional_parameters); 5239 params->has_optional_positional_parameters);
5238 const int num_parameters = params->parameters->length(); 5240 const int num_parameters = params->parameters->length();
5239 ASSERT(num_parameters == func.NumParameters()); 5241 ASSERT(num_parameters == func.NumParameters());
5240 func.set_parameter_types(Array::Handle(Array::New(num_parameters, 5242 func.set_parameter_types(Array::Handle(Array::New(num_parameters,
5241 Heap::kOld))); 5243 Heap::kOld)));
5242 func.set_parameter_names(Array::Handle(Array::New(num_parameters, 5244 func.set_parameter_names(Array::Handle(Array::New(num_parameters,
(...skipping 1983 matching lines...) Expand 10 before | Expand all | Expand 10 after
7226 new LoadLocalNode(statement_pos, excp_var), 7228 new LoadLocalNode(statement_pos, excp_var),
7227 new LoadLocalNode(statement_pos, trace_var)); 7229 new LoadLocalNode(statement_pos, trace_var));
7228 } else { 7230 } else {
7229 statement = ParseExpr(kAllowConst, kConsumeCascades); 7231 statement = ParseExpr(kAllowConst, kConsumeCascades);
7230 ExpectSemicolon(); 7232 ExpectSemicolon();
7231 } 7233 }
7232 return statement; 7234 return statement;
7233 } 7235 }
7234 7236
7235 7237
7236 RawError* Parser::FormatErrorWithAppend(const Error& prev_error,
7237 const Script& script,
7238 intptr_t token_pos,
7239 const char* message_header,
7240 const char* format,
7241 va_list args) {
7242 const String& msg1 = String::Handle(String::New(prev_error.ToErrorCString()));
7243 const String& msg2 = String::Handle(
7244 FormatMessage(script, token_pos, message_header, format, args));
7245 return LanguageError::New(String::Handle(String::Concat(msg1, msg2)));
7246 }
7247
7248
7249 RawError* Parser::FormatError(const Script& script,
7250 intptr_t token_pos,
7251 const char* message_header,
7252 const char* format,
7253 va_list args) {
7254 const String& msg = String::Handle(
7255 FormatMessage(script, token_pos, message_header, format, args));
7256 return LanguageError::New(msg);
7257 }
7258
7259
7260 RawError* Parser::FormatErrorMsg(const Script& script,
7261 intptr_t token_pos,
7262 const char* message_header,
7263 const char* format, ...) {
7264 va_list args;
7265 va_start(args, format);
7266 const Error& error = Error::Handle(
7267 FormatError(script, token_pos, message_header, format, args));
7268 va_end(args);
7269 return error.raw();
7270 }
7271
7272
7273 RawString* Parser::FormatMessage(const Script& script,
7274 intptr_t token_pos,
7275 const char* message_header,
7276 const char* format, va_list args) {
7277 String& result = String::Handle();
7278 const String& msg = String::Handle(String::NewFormattedV(format, args));
7279 if (!script.IsNull()) {
7280 const String& script_url = String::Handle(script.url());
7281 if (token_pos >= 0) {
7282 intptr_t line, column;
7283 script.GetTokenLocation(token_pos, &line, &column);
7284 // Only report the line position if we have the original source. We still
7285 // need to get a valid column so that we can report the ^ mark below the
7286 // snippet.
7287 if (script.HasSource()) {
7288 result = String::NewFormatted("'%s': %s: line %" Pd " pos %" Pd ": ",
7289 script_url.ToCString(),
7290 message_header,
7291 line,
7292 column);
7293 } else {
7294 result = String::NewFormatted("'%s': %s: line %" Pd ": ",
7295 script_url.ToCString(),
7296 message_header,
7297 line);
7298 }
7299 // Append the formatted error or warning message.
7300 result = String::Concat(result, msg);
7301 const String& new_line = String::Handle(String::New("\n"));
7302 // Append the source line.
7303 const String& script_line = String::Handle(script.GetLine(line));
7304 ASSERT(!script_line.IsNull());
7305 result = String::Concat(result, new_line);
7306 result = String::Concat(result, script_line);
7307 result = String::Concat(result, new_line);
7308 // Append the column marker.
7309 const String& column_line = String::Handle(
7310 String::NewFormatted("%*s\n", static_cast<int>(column), "^"));
7311 result = String::Concat(result, column_line);
7312 } else {
7313 // Token position is unknown.
7314 result = String::NewFormatted("'%s': %s: ",
7315 script_url.ToCString(),
7316 message_header);
7317 result = String::Concat(result, msg);
7318 }
7319 } else {
7320 // Script is unknown.
7321 // Append the formatted error or warning message.
7322 result = msg.raw();
7323 }
7324 return result.raw();
7325 }
7326
7327
7328 void Parser::PrintMessage(const Script& script,
7329 intptr_t token_pos,
7330 const char* message_header,
7331 const char* format, ...) {
7332 va_list args;
7333 va_start(args, format);
7334 const String& buf = String::Handle(
7335 FormatMessage(script, token_pos, message_header, format, args));
7336 va_end(args);
7337 OS::Print("%s", buf.ToCString());
7338 }
7339
7340
7341 void Parser::ErrorMsg(intptr_t token_pos, const char* format, ...) const { 7238 void Parser::ErrorMsg(intptr_t token_pos, const char* format, ...) const {
7342 va_list args; 7239 va_list args;
7343 va_start(args, format); 7240 va_start(args, format);
7344 const Error& error = Error::Handle( 7241 const Error& error = Error::Handle(LanguageError::NewFormattedV(
7345 FormatError(script_, token_pos, "Error", format, args)); 7242 Error::Handle(), script_, token_pos,
7243 LanguageError::kError, Heap::kNew, format, args));
7346 va_end(args); 7244 va_end(args);
7347 isolate()->long_jump_base()->Jump(1, error); 7245 isolate()->long_jump_base()->Jump(1, error);
7348 UNREACHABLE(); 7246 UNREACHABLE();
7349 } 7247 }
7350 7248
7351 7249
7352 void Parser::ErrorMsg(const char* format, ...) { 7250 void Parser::ErrorMsg(const char* format, ...) {
7353 va_list args; 7251 va_list args;
7354 va_start(args, format); 7252 va_start(args, format);
7355 const Error& error = Error::Handle( 7253 const Error& error = Error::Handle(LanguageError::NewFormattedV(
7356 FormatError(script_, TokenPos(), "Error", format, args)); 7254 Error::Handle(), script_, TokenPos(),
7255 LanguageError::kError, Heap::kNew, format, args));
7357 va_end(args); 7256 va_end(args);
7358 isolate()->long_jump_base()->Jump(1, error); 7257 isolate()->long_jump_base()->Jump(1, error);
7359 UNREACHABLE(); 7258 UNREACHABLE();
7360 } 7259 }
7361 7260
7362 7261
7363 void Parser::ErrorMsg(const Error& error) { 7262 void Parser::ErrorMsg(const Error& error) {
7364 Isolate::Current()->long_jump_base()->Jump(1, error); 7263 Isolate::Current()->long_jump_base()->Jump(1, error);
7365 UNREACHABLE(); 7264 UNREACHABLE();
7366 } 7265 }
7367 7266
7368 7267
7369 void Parser::AppendErrorMsg( 7268 void Parser::AppendErrorMsg(
7370 const Error& prev_error, intptr_t token_pos, const char* format, ...) { 7269 const Error& prev_error, intptr_t token_pos, const char* format, ...) {
7371 va_list args; 7270 va_list args;
7372 va_start(args, format); 7271 va_start(args, format);
7373 const Error& error = Error::Handle(FormatErrorWithAppend( 7272 const Error& error = Error::Handle(
7374 prev_error, script_, token_pos, "Error", format, args)); 7273 LanguageError::NewFormattedV(
7274 prev_error, script_, token_pos,
7275 LanguageError::kError, Heap::kNew,
7276 format, args));
7375 va_end(args); 7277 va_end(args);
7376 isolate()->long_jump_base()->Jump(1, error); 7278 isolate()->long_jump_base()->Jump(1, error);
7377 UNREACHABLE(); 7279 UNREACHABLE();
7378 } 7280 }
7379 7281
7380 7282
7381 void Parser::Warning(intptr_t token_pos, const char* format, ...) { 7283 void Parser::Warning(intptr_t token_pos, const char* format, ...) {
7382 if (FLAG_silent_warnings) return; 7284 if (FLAG_silent_warnings) return;
7383 va_list args; 7285 va_list args;
7384 va_start(args, format); 7286 va_start(args, format);
7385 const Error& error = Error::Handle( 7287 const Error& error = Error::Handle(
7386 FormatError(script_, token_pos, "Warning", format, args)); 7288 LanguageError::NewFormattedV(
7289 Error::Handle(), script_, token_pos,
7290 LanguageError::kWarning, Heap::kNew,
7291 format, args));
7387 va_end(args); 7292 va_end(args);
7388 if (FLAG_warning_as_error) { 7293 if (FLAG_warning_as_error) {
7389 isolate()->long_jump_base()->Jump(1, error); 7294 isolate()->long_jump_base()->Jump(1, error);
7390 UNREACHABLE(); 7295 UNREACHABLE();
7391 } else { 7296 } else {
7392 OS::Print("%s", error.ToErrorCString()); 7297 OS::Print("%s", error.ToErrorCString());
7393 } 7298 }
7394 } 7299 }
7395 7300
7396 7301
7397 void Parser::Warning(const char* format, ...) { 7302 void Parser::Warning(const char* format, ...) {
7398 if (FLAG_silent_warnings) return; 7303 if (FLAG_silent_warnings) return;
7399 va_list args; 7304 va_list args;
7400 va_start(args, format); 7305 va_start(args, format);
7401 const Error& error = Error::Handle( 7306 const Error& error = Error::Handle(
7402 FormatError(script_, TokenPos(), "Warning", format, args)); 7307 LanguageError::NewFormattedV(
7308 Error::Handle(), script_, TokenPos(),
7309 LanguageError::kWarning, Heap::kNew,
7310 format, args));
7403 va_end(args); 7311 va_end(args);
7404 if (FLAG_warning_as_error) { 7312 if (FLAG_warning_as_error) {
7405 isolate()->long_jump_base()->Jump(1, error); 7313 isolate()->long_jump_base()->Jump(1, error);
7406 UNREACHABLE(); 7314 UNREACHABLE();
7407 } else { 7315 } else {
7408 OS::Print("%s", error.ToErrorCString()); 7316 OS::Print("%s", error.ToErrorCString());
7409 } 7317 }
7410 } 7318 }
7411 7319
7412 7320
(...skipping 3378 matching lines...) Expand 10 before | Expand all | Expand 10 after
10791 void Parser::SkipQualIdent() { 10699 void Parser::SkipQualIdent() {
10792 ASSERT(IsIdentifier()); 10700 ASSERT(IsIdentifier());
10793 ConsumeToken(); 10701 ConsumeToken();
10794 if (CurrentToken() == Token::kPERIOD) { 10702 if (CurrentToken() == Token::kPERIOD) {
10795 ConsumeToken(); // Consume the kPERIOD token. 10703 ConsumeToken(); // Consume the kPERIOD token.
10796 ExpectIdentifier("identifier expected after '.'"); 10704 ExpectIdentifier("identifier expected after '.'");
10797 } 10705 }
10798 } 10706 }
10799 10707
10800 } // namespace dart 10708 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/raw_object.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698