Christopher Wiley | b8b59f6 | 2015-09-15 12:19:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 17 | #pragma once |
Christopher Wiley | b8b59f6 | 2015-09-15 12:19:53 -0700 | [diff] [blame] | 18 | |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 19 | #include "aidl_language.h" |
| 20 | |
| 21 | #include <iostream> |
| 22 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 23 | // We must include windows.h before android-base/logging.h on Windows. |
Christopher Wiley | b8b59f6 | 2015-09-15 12:19:53 -0700 | [diff] [blame] | 24 | #ifdef _WIN32 |
| 25 | #include <windows.h> |
| 26 | #endif |
| 27 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Jiyong Park | 2a7c92b | 2020-07-22 19:12:36 +0900 | [diff] [blame] | 29 | |
| 30 | // Generic point for printing any error in the AIDL compiler. |
| 31 | class AidlErrorLog { |
| 32 | public: |
| 33 | AidlErrorLog(bool fatal, const AidlLocation& location) |
| 34 | : os_(std::cerr), fatal_(fatal), location_(location) { |
| 35 | sHadError = true; |
| 36 | |
| 37 | os_ << "ERROR: "; |
| 38 | os_ << location << ": "; |
| 39 | } |
| 40 | AidlErrorLog(bool fatal, const std::string& filename) |
| 41 | : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {} |
| 42 | AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {} |
| 43 | AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {} |
| 44 | |
| 45 | template <typename T> |
| 46 | AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {} |
| 47 | ~AidlErrorLog() { |
| 48 | os_ << std::endl; |
| 49 | if (fatal_) abort(); |
| 50 | if (location_.IsInternal()) { |
| 51 | os_ << "Logging an internal location should not happen. Offending location: " << location_ |
| 52 | << std::endl; |
| 53 | abort(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // AidlErrorLog is a single use object. No need to copy or move |
| 58 | AidlErrorLog(const AidlErrorLog&) = delete; |
| 59 | AidlErrorLog(AidlErrorLog&&) = delete; |
| 60 | AidlErrorLog& operator=(const AidlErrorLog&) = delete; |
| 61 | AidlErrorLog& operator=(AidlErrorLog&&) = delete; |
| 62 | |
| 63 | std::ostream& os_; |
| 64 | |
| 65 | static void clearError() { sHadError = false; } |
| 66 | static bool hadError() { return sHadError; } |
| 67 | |
| 68 | private: |
| 69 | bool fatal_; |
| 70 | const AidlLocation location_; |
| 71 | static bool sHadError; |
| 72 | }; |
| 73 | |
| 74 | // A class used to make it obvious to clang that code is going to abort. This |
| 75 | // informs static analyses of the aborting behavior of `AIDL_FATAL`, and |
| 76 | // helps generate slightly faster/smaller code. |
| 77 | class AidlAbortOnDestruction { |
| 78 | public: |
| 79 | __attribute__((noreturn)) ~AidlAbortOnDestruction() { abort(); } |
| 80 | }; |
| 81 | |
| 82 | #define AIDL_ERROR(CONTEXT) ::AidlErrorLog(false /*fatal*/, (CONTEXT)).os_ |
| 83 | #define AIDL_FATAL(CONTEXT) \ |
| 84 | (::AidlAbortOnDestruction(), ::AidlErrorLog(true /*fatal*/, (CONTEXT)).os_) |
| 85 | #define AIDL_FATAL_IF(CONDITION, CONTEXT) \ |
| 86 | if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": " |