1 /*
2  * Copyright (C) 2017 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 namespace slicer {
20 
21 // Encapsulate the runtime check and error reporting policy.
22 // (currently a simple fail-fast but the the intention is to allow customization)
23 void _checkFailed(const char* expr, int line, const char* file) __attribute__((noreturn));
24 #define SLICER_CHECK(expr) do { if(!(expr)) slicer::_checkFailed(#expr, __LINE__, __FILE__); } while(false)
25 
26 // A modal check: if the strict mode is enabled, it behaves as a SLICER_CHECK,
27 // otherwise it will only log a warning and continue
28 //
29 // NOTE: we use SLICER_WEAK_CHECK for .dex format validations that are frequently
30 //   violated by existing apps. So we need to be able to annotate these common
31 //   problems and potentially ignoring them for parity with the Android runtime.
32 //
33 void _weakCheckFailed(const char* expr, int line, const char* file);
34 #define SLICER_WEAK_CHECK(expr) do { if(!(expr)) slicer::_weakCheckFailed(#expr, __LINE__, __FILE__); } while(false)
35 
36 // Report a fatal condition with a printf-formatted message
37 void _fatal(const char* format, ...) __attribute__((noreturn));
38 #define SLICER_FATAL(format, ...) slicer::_fatal("\nSLICER_FATAL: " format "\n\n", ##__VA_ARGS__);
39 
40 // Annotation customization point for extra validation / state.
41 #ifdef NDEBUG
42 #define SLICER_EXTRA(x)
43 #else
44 #define SLICER_EXTRA(x) x
45 #endif
46 
47 #ifndef FALLTHROUGH_INTENDED
48 #ifdef __clang__
49 #define FALLTHROUGH_INTENDED [[clang::fallthrough]]
50 #else
51 #define FALLTHROUGH_INTENDED
52 #endif // __clang__
53 #endif // FALLTHROUGH_INTENDED
54 
55 } // namespace slicer
56 
57