• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
20 #include <string>
21 
22 #include "location.h"
23 
24 class AidlNode;
25 
26 // Generic point for printing any error in the AIDL compiler.
27 class AidlErrorLog {
28  public:
29   enum Severity { NO_OP, WARNING, ERROR, FATAL };
30 
31   AidlErrorLog(Severity severity, const AidlLocation& location, const std::string& suffix = "");
32   AidlErrorLog(Severity severity, const std::string& filename);
33   AidlErrorLog(Severity severity, const AidlNode& node);
AidlErrorLog(Severity severity,const AidlNode * node)34   AidlErrorLog(Severity severity, const AidlNode* node) : AidlErrorLog(severity, *node) {}
35 
36   template <typename T>
AidlErrorLog(Severity severity,const std::unique_ptr<T> & node)37   AidlErrorLog(Severity severity, const std::unique_ptr<T>& node) : AidlErrorLog(severity, *node) {}
38   ~AidlErrorLog();
39 
40   // AidlErrorLog is a single use object. No need to copy
41   AidlErrorLog(const AidlErrorLog&) = delete;
42   AidlErrorLog& operator=(const AidlErrorLog&) = delete;
43 
44   // btw, making it movable so that functions can return it.
45   AidlErrorLog(AidlErrorLog&&) = default;
46   AidlErrorLog& operator=(AidlErrorLog&&) = delete;
47 
48   template <typename T>
49   AidlErrorLog& operator<<(T&& arg) {
50     if (severity_ != NO_OP) {
51       (*os_) << std::forward<T>(arg);
52     }
53     return *this;
54   }
55 
clearError()56   static void clearError() { sHadError = false; }
hadError()57   static bool hadError() { return sHadError; }
58 
59  private:
60   std::ostream* os_;
61   Severity severity_;
62   const AidlLocation location_;
63   const std::string suffix_;
64   static bool sHadError;
65 };
66 
67 // A class used to make it obvious to clang that code is going to abort. This
68 // informs static analyses of the aborting behavior of `AIDL_FATAL`, and
69 // helps generate slightly faster/smaller code.
70 class AidlAbortOnDestruction {
71  public:
~AidlAbortOnDestruction()72   __attribute__((noreturn)) ~AidlAbortOnDestruction() { abort(); }
73 };
74 
75 #define AIDL_ERROR(CONTEXT) ::AidlErrorLog(AidlErrorLog::ERROR, (CONTEXT))
76 #define AIDL_FATAL(CONTEXT) \
77   (::AidlAbortOnDestruction(), ::AidlErrorLog(AidlErrorLog::FATAL, (CONTEXT)))
78 #define AIDL_FATAL_IF(CONDITION, CONTEXT) \
79   if (CONDITION) AIDL_FATAL(CONTEXT) << "Bad internal state: " << #CONDITION << ": "
80