1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_ERR_H_ 6 #define TOOLS_GN_ERR_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "gn/label.h" 13 #include "gn/location.h" 14 #include "gn/token.h" 15 16 class ParseNode; 17 class Value; 18 19 // Result of doing some operation. Check has_error() to see if an error 20 // occurred. 21 // 22 // An error has a location and a message. Below that, is some optional help 23 // text to go with the annotation of the location. 24 // 25 // An error can also have sub-errors which are additionally printed out 26 // below. They can provide additional context. 27 class Err { 28 private: 29 struct ErrInfo { ErrInfoErrInfo30 ErrInfo(const Location& loc, 31 const std::string& msg, 32 const std::string& help) 33 : location(loc), message(msg), help_text(help) {} 34 35 Location location; 36 Label toolchain_label; 37 38 std::vector<LocationRange> ranges; 39 40 std::string message; 41 std::string help_text; 42 43 std::vector<Err> sub_errs; 44 }; 45 46 public: 47 using RangeList = std::vector<LocationRange>; 48 49 // Indicates no error. 50 Err() = default; 51 52 // Error at a single point. 53 Err(const Location& location, 54 const std::string& msg, 55 const std::string& help = std::string()); 56 57 // Error at a given range. 58 Err(const LocationRange& range, 59 const std::string& msg, 60 const std::string& help = std::string()); 61 62 // Error at a given token. 63 Err(const Token& token, 64 const std::string& msg, 65 const std::string& help_text = std::string()); 66 67 // Error at a given node. 68 Err(const ParseNode* node, 69 const std::string& msg, 70 const std::string& help_text = std::string()); 71 72 // Error at a given value. 73 Err(const Value& value, 74 const std::string& msg, 75 const std::string& help_text = std::string()); 76 77 Err(const Err& other); 78 Err(Err&& other) = default; 79 80 Err& operator=(const Err&); 81 Err& operator=(Err&&) = default; 82 has_error()83 bool has_error() const { return !!info_; } 84 85 // All getters and setters below require has_error() returns true. location()86 const Location& location() const { return info_->location; } message()87 const std::string& message() const { return info_->message; } help_text()88 const std::string& help_text() const { return info_->help_text; } 89 AppendRange(const LocationRange & range)90 void AppendRange(const LocationRange& range) { 91 info_->ranges.push_back(range); 92 } ranges()93 const RangeList& ranges() const { return info_->ranges; } 94 set_toolchain_label(const Label & toolchain_label)95 void set_toolchain_label(const Label& toolchain_label) { 96 info_->toolchain_label = toolchain_label; 97 } 98 99 void AppendSubErr(const Err& err); 100 101 void PrintToStdout() const; 102 103 // Prints to standard out but uses a "WARNING" messaging instead of the 104 // normal "ERROR" messaging. This is a property of the printing system rather 105 // than of the Err class because there is no expectation that code calling a 106 // function that take an Err check that the error is nonfatal and continue. 107 // Generally all Err objects with has_error() set are fatal. 108 // 109 // In some very specific cases code will detect a condition and print a 110 // nonfatal error to the screen instead of returning it. In these cases, that 111 // code can decide at printing time whether it will continue (and use this 112 // method) or not (and use PrintToStdout()). 113 void PrintNonfatalToStdout() const; 114 115 private: 116 void InternalPrintToStdout(bool is_sub_err, bool is_fatal) const; 117 118 std::unique_ptr<ErrInfo> info_; // Non-null indicates error. 119 }; 120 121 #endif // TOOLS_GN_ERR_H_ 122