error_builders.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_COMMON_ERROR_BUILDERS_H_
  5. #define EXECUTABLE_SEMANTICS_COMMON_ERROR_BUILDERS_H_
  6. #include "common/error.h"
  7. #include "executable_semantics/common/source_location.h"
  8. namespace Carbon {
  9. // Builds an Error instance with the specified message. This should be used for
  10. // non-recoverable errors with user input.
  11. //
  12. // For example:
  13. // return ProgramError(line_num) << "Line is bad!";
  14. // return ProgramError() << "Application is bad!";
  15. //
  16. // Where possible, try to identify the error as a compilation or runtime error.
  17. // Use CHECK/FATAL for internal errors. The generic program error option is
  18. // provided as a fallback for cases that don't fit those classifications.
  19. inline auto CompilationError(SourceLocation loc) -> ErrorBuilder {
  20. ErrorBuilder builder;
  21. (void)(builder << "COMPILATION ERROR: " << loc << ": ");
  22. return builder;
  23. }
  24. inline auto ProgramError(SourceLocation loc) -> ErrorBuilder {
  25. ErrorBuilder builder;
  26. (void)(builder << "PROGRAM ERROR: " << loc << ": ");
  27. return builder;
  28. }
  29. inline auto RuntimeError(SourceLocation loc) -> ErrorBuilder {
  30. ErrorBuilder builder;
  31. (void)(builder << "RUNTIME ERROR: " << loc << ": ");
  32. return builder;
  33. }
  34. } // namespace Carbon
  35. #endif // EXECUTABLE_SEMANTICS_COMMON_ERROR_BUILDERS_H_