1 /* Automatic generation of links into GCC's documentation.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
24 #include "pretty-print.h"
25 #include "pretty-print-urlifier.h"
26 #include "gcc-urlifier.h"
31 /* Concrete subclass of urlifier for generating links into
32 GCC's HTML documentation. */
34 class gcc_urlifier
: public urlifier
37 char *get_url_for_quoted_text (const char *p
, size_t sz
) const final override
;
39 const char *get_url_suffix_for_quoted_text (const char *p
, size_t sz
) const;
40 /* We use ATTRIBUTE_UNUSED as this helper is called only from ASSERTs. */
41 const char *get_url_suffix_for_quoted_text (const char *p
) const ATTRIBUTE_UNUSED
;
45 make_doc_url (const char *doc_url_suffix
);
48 /* class gcc_urlifier : public urlifier. */
50 #define DOC_URL(QUOTED_TEXT, URL_SUFFIX) \
51 { (QUOTED_TEXT), (URL_SUFFIX) }
55 const char *quoted_text
;
56 const char *url_suffix
;
59 #include "gcc-urlifier.def"
64 gcc_urlifier::get_url_for_quoted_text (const char *p
, size_t sz
) const
66 if (const char *url_suffix
= get_url_suffix_for_quoted_text (p
, sz
))
67 return make_doc_url (url_suffix
);
72 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p
, size_t sz
) const
74 /* Binary search. This assumes that the quoted_text fields of doc_urls
75 are in sorted order. */
77 int max
= ARRAY_SIZE (doc_urls
) - 1;
82 int midpoint
= (min
+ max
) / 2;
83 gcc_assert ((size_t)midpoint
< ARRAY_SIZE (doc_urls
));
84 int cmp
= strncmp (p
, doc_urls
[midpoint
].quoted_text
, sz
);
87 if (doc_urls
[midpoint
].quoted_text
[sz
] == '\0')
88 return doc_urls
[midpoint
].url_suffix
;
101 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p
) const
103 return get_url_suffix_for_quoted_text (p
, strlen (p
));
107 gcc_urlifier::make_doc_url (const char *doc_url_suffix
)
112 return concat (DOCUMENTATION_ROOT_URL
, doc_url_suffix
, nullptr);
115 } // anonymous namespace
120 return new gcc_urlifier ();
129 /* Run all of the selftests within this file. */
132 gcc_urlifier_cc_tests ()
134 /* Check that doc_urls.quoted_text is sorted. */
135 for (size_t idx
= 1; idx
< ARRAY_SIZE (doc_urls
); idx
++)
136 gcc_assert (strcmp (doc_urls
[idx
- 1].quoted_text
,
137 doc_urls
[idx
].quoted_text
)
142 ASSERT_EQ (u
.get_url_suffix_for_quoted_text (""), nullptr);
143 ASSERT_EQ (u
.get_url_suffix_for_quoted_text (")"), nullptr);
145 ASSERT_STREQ (u
.get_url_suffix_for_quoted_text ("#pragma message"),
146 "gcc/Diagnostic-Pragmas.html");
148 // Incomplete prefix of a quoted_text
149 ASSERT_EQ (u
.get_url_suffix_for_quoted_text ("#pragma mess"), nullptr);
151 /* Check that every element is findable. */
152 for (size_t idx
= 0; idx
< ARRAY_SIZE (doc_urls
); idx
++)
154 (u
.get_url_suffix_for_quoted_text (doc_urls
[idx
].quoted_text
),
155 doc_urls
[idx
].url_suffix
);
158 } // namespace selftest
160 #endif /* #if CHECKING_P */