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, const std::string& msg, const std::string& help) 31 : location(loc), message(msg), help_text(help) {} 32 33 Location location; 34 Label toolchain_label; 35 36 std::vector<LocationRange> ranges; 37 38 std::string message; 39 std::string help_text; 40 41 std::vector<Err> sub_errs; 42 }; 43 44 public: 45 using RangeList = std::vector<LocationRange>; 46 47 // Indicates no error. 48 Err() = default; 49 50 // Error at a single point. 51 Err(const Location& location, 52 const std::string& msg, 53 const std::string& help = std::string()); 54 55 // Error at a given range. 56 Err(const LocationRange& range, 57 const std::string& msg, 58 const std::string& help = std::string()); 59 60 // Error at a given token. 61 Err(const Token& token, 62 const std::string& msg, 63 const std::string& help_text = std::string()); 64 65 // Error at a given node. 66 Err(const ParseNode* node, 67 const std::string& msg, 68 const std::string& help_text = std::string()); 69 70 // Error at a given value. 71 Err(const Value& value, 72 const std::string& msg, 73 const std::string& help_text = std::string()); 74 75 Err(const Err& other); 76 Err(Err&& other) = default; 77 78 Err& operator=(const Err&); 79 Err& operator=(Err&&) = default; 80 has_error()81 bool has_error() const { return !!info_; } 82 83 // All getters and setters below require has_error() returns true. location()84 const Location& location() const { return info_->location; } message()85 const std::string& message() const { return info_->message; } help_text()86 const std::string& help_text() const { return info_->help_text; } 87 AppendRange(const LocationRange & range)88 void AppendRange(const LocationRange& range) { info_->ranges.push_back(range); } ranges()89 const RangeList& ranges() const { return info_->ranges; } 90 set_toolchain_label(const Label & toolchain_label)91 void set_toolchain_label(const Label& toolchain_label) { 92 info_->toolchain_label = toolchain_label; 93 } 94 95 void AppendSubErr(const Err& err); 96 97 void PrintToStdout() const; 98 99 // Prints to standard out but uses a "WARNING" messaging instead of the 100 // normal "ERROR" messaging. This is a property of the printing system rather 101 // than of the Err class because there is no expectation that code calling a 102 // function that take an Err check that the error is nonfatal and continue. 103 // Generally all Err objects with has_error() set are fatal. 104 // 105 // In some very specific cases code will detect a condition and print a 106 // nonfatal error to the screen instead of returning it. In these cases, that 107 // code can decide at printing time whether it will continue (and use this 108 // method) or not (and use PrintToStdout()). 109 void PrintNonfatalToStdout() const; 110 111 private: 112 void InternalPrintToStdout(bool is_sub_err, bool is_fatal) const; 113 114 std::unique_ptr<ErrInfo> info_; // Non-null indicates error. 115 }; 116 117 #endif // TOOLS_GN_ERR_H_ 118