1 //===-- lib/Common/idioms.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Common/idioms.h"
10 #include <cstdarg>
11 #include <cstdio>
12 #include <cstdlib>
13 
14 namespace Fortran::common {
15 
16 [[noreturn]] void die(const char *msg, ...) {
17   va_list ap;
18   va_start(ap, msg);
19   std::fputs("\nfatal internal error: ", stderr);
_int_contained(PG_FUNCTION_ARGS)20   std::vfprintf(stderr, msg, ap);
21   va_end(ap);
22   fputc('\n', stderr);
23   std::abort();
24 }
25 
26 // Convert the int index of an enumerator to a string.
27 // enumNames is a list of the names, separated by commas with optional spaces.
28 // This is intended for use from the expansion of ENUM_CLASS.
_int_contains(PG_FUNCTION_ARGS)29 std::string EnumIndexToString(int index, const char *enumNames) {
30   const char *p{enumNames};
31   for (; index > 0; --index, ++p) {
32     for (; *p && *p != ','; ++p) {
33     }
34   }
35   for (; *p == ' '; ++p) {
36   }
37   CHECK(*p != '\0');
38   const char *q = p;
39   for (; *q && *q != ' ' && *q != ','; ++q) {
40   }
41   return std::string(p, q - p);
42 }
43 } // namespace Fortran::common
44