| 1 | //===-------------------------------------------------------------- C++ -*-===// |
| 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 "llvm/Frontend/Directive/Spelling.h" |
| 10 | |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/Support/MathExtras.h" |
| 13 | |
| 14 | #include <cassert> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | static bool Contains(directive::VersionRange V, int P) { |
| 19 | return V.Min <= P && P <= V.Max; |
| 20 | } |
| 21 | |
| 22 | llvm::StringRef llvm::directive::FindName( |
| 23 | llvm::iterator_range<const directive::Spelling *> Range, unsigned Version) { |
| 24 | assert(llvm::isInt<8 * sizeof(int)>(Version) && "Version value out of range" ); |
| 25 | |
| 26 | int V = Version; |
| 27 | // Do a linear search to find the first Spelling that contains Version. |
| 28 | // The condition "contains(S, Version)" does not partition the list of |
| 29 | // spellings, so std::[lower|upper]_bound cannot be used. |
| 30 | // In practice the list of spellings is expected to be very short, so |
| 31 | // linear search seems appropriate. In general, an interval tree may be |
| 32 | // a better choice, but in this case it may be an overkill. |
| 33 | for (auto &S : Range) { |
| 34 | if (Contains(V: S.Versions, P: V)) |
| 35 | return S.Name; |
| 36 | } |
| 37 | return StringRef(); |
| 38 | } |
| 39 | |