libctf, hash: introduce the ctf_dynset
[deliverable/binutils-gdb.git] / libctf / ctf-error.c
1 /* Error table.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3
4 This file is part of libctf.
5
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <ctf-impl.h>
21 #include <stddef.h>
22
23 /* This construct is due to Bruno Haible: much thanks. */
24
25 /* Give each structure member a unique name. The name does not matter, so we
26 use the line number in ctf-error.h to uniquify them. */
27
28 #define ERRSTRFIELD(line) ERRSTRFIELD1 (line)
29 #define ERRSTRFIELD1(line) ctf_errstr##line
30
31 /* The error message strings, each in a unique structure member precisely big
32 enough for that error, plus a str member to access them all as a string
33 table. */
34
35 static const union _ctf_errlist_t
36 {
37 __extension__ struct
38 {
39 #define _CTF_STR(n, s) char ERRSTRFIELD (__LINE__) [sizeof (s)];
40 #include "ctf-error.h"
41 #undef _CTF_STR
42 };
43 char str[1];
44 } _ctf_errlist =
45 {
46 {
47 #define _CTF_STR(n, s) s,
48 #include "ctf-error.h"
49 #undef _CTF_STR
50 }
51 };
52
53 /* Offsets to each member in the string table, computed using offsetof. */
54
55 static const unsigned int _ctf_erridx[] =
56 {
57 #define _CTF_STR(n, s) [n - ECTF_BASE] = offsetof (union _ctf_errlist_t, ERRSTRFIELD (__LINE__)),
58 #include "ctf-error.h"
59 #undef _CTF_STR
60 };
61
62 const char *
63 ctf_errmsg (int error)
64 {
65 const char *str;
66
67 if (error >= ECTF_BASE && (error - ECTF_BASE) < ECTF_NERR)
68 str = _ctf_errlist.str + _ctf_erridx[error - ECTF_BASE];
69 else
70 str = ctf_strerror (error);
71
72 return (str ? str : "Unknown error");
73 }
74
75 int
76 ctf_errno (ctf_file_t * fp)
77 {
78 return fp->ctf_errno;
79 }
This page took 0.030543 seconds and 4 git commands to generate.