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 
17 #pragma once
18 
19 #include "aidl_language.h"
20 
21 #include <iostream>
22 
23 // We must include windows.h before android-base/logging.h on Windows.
24 #ifdef _WIN32
25 #include <windows.h>
26 #endif
27 
28 #include <android-base/logging.h>
29 
30 // Generic point for printing any error in the AIDL compiler.
31 class AidlErrorLog {
32  public:
AidlErrorLog(bool fatal,const AidlLocation & location)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   }
AidlErrorLog(bool fatal,const std::string & filename)40   AidlErrorLog(bool fatal, const std::string& filename)
41       : AidlErrorLog(fatal, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
AidlErrorLog(bool fatal,const AidlNode & node)42   AidlErrorLog(bool fatal, const AidlNode& node) : AidlErrorLog(fatal, node.location_) {}
AidlErrorLog(bool fatal,const AidlNode * node)43   AidlErrorLog(bool fatal, const AidlNode* node) : AidlErrorLog(fatal, *node) {}
44 
45   template <typename T>
AidlErrorLog(bool fatal,const std::unique_ptr<T> & node)46   AidlErrorLog(bool fatal, const std::unique_ptr<T>& node) : AidlErrorLog(fatal, *node) {}
~AidlErrorLog()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 
clearError()65   static void clearError() { sHadError = false; }
hadError()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:
~AidlAbortOnDestruction()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 << ": "
87