llvm/flang/lib/parser/idioms.cc
Tim Keith 9f755666fb [flang] Add ENUM_CLASS to define enum class with ToString function.
This replaces DEFINE_NESTED_ENUM_CLASS in parse-tree.h but works
similarly. "ENUM_CLASS(Foo, A, B, C)" defined enum class Foo with
enumerators A, B, C. It also defines an overloading of EnumToString
that converts enumerators to their string representation.

Change unparse.cc to adapt to this change.

Make use of ENUM_CLASS in attr.h and attr.cc.

Original-commit: flang-compiler/f18@c45b8f172a
Reviewed-on: https://github.com/flang-compiler/f18/pull/31
2018-03-23 14:41:48 -07:00

38 lines
914 B
C++

#include "idioms.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
namespace Fortran {
namespace parser {
[[noreturn]] void die(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
std::fputs("\nfatal internal error: ", stderr);
std::vfprintf(stderr, msg, ap);
va_end(ap);
fputc('\n', stderr);
std::abort();
}
// Convert the int index of an enumerator to a string.
// enumNames is a list of the names, separated by commas with optional spaces.
// This is intended for use from the expansion of ENUM_CLASS.
std::string EnumIndexToString(int index, const char *enumNames) {
const char *p{enumNames};
for (; index > 0; --index, ++p) {
for (; *p && *p != ','; ++p) {
}
}
for (; *p == ' '; ++p) {
}
CHECK(*p != '\0');
const char *q = p;
for (; *q && *q != ' ' && *q != ','; ++q) {
}
return std::string(p, q - p);
}
} // namespace parser
} // namespace Fortran