Skip to content

Commit 0ea1367

Browse files
author
Jethro Beekman
committed
Fix tests with LLVM 4.0 by checking for LLVM bug 9069
1 parent 3060816 commit 0ea1367

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

tests/clang.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ unsafe fn location_in_scope(r: CXSourceRange) -> bool {
110110
&& file.0!=ptr::null_mut()
111111
}
112112

113-
fn test_file(file: &str) -> bool {
114-
let mut idents=HashMap::new();
115-
let mut all_succeeded=true;
113+
/// tokenize_range_adjust can be used to work around LLVM bug 9069
114+
/// https://bugs.llvm.org//show_bug.cgi?id=9069
115+
fn file_visit_macros<F: FnMut(Vec<u8>, Vec<Token>)>(file: &str, tokenize_range_adjust: bool, mut visitor: F) {
116116
unsafe {
117117
let tu={
118118
let index=clang_createIndex(true as _, false as _);
@@ -132,7 +132,7 @@ fn test_file(file: &str) -> bool {
132132
if cur.kind==CXCursor_MacroDefinition {
133133
let mut range=clang_getCursorExtent(cur);
134134
if !location_in_scope(range) { return CXChildVisit_Continue }
135-
range.end_int_data-=1; // clang bug for macros only
135+
range.end_int_data-=if tokenize_range_adjust { 1 } else { 0 };
136136
let mut token_ptr=ptr::null_mut();
137137
let mut num=0;
138138
clang_tokenize(tu,range,&mut token_ptr,&mut num);
@@ -146,16 +146,44 @@ fn test_file(file: &str) -> bool {
146146
}
147147
).collect();
148148
clang_disposeTokens(tu,token_ptr,num);
149-
all_succeeded&=test_definition(clang_str_to_vec(clang_getCursorSpelling(cur)),&tokens,&mut idents);
149+
visitor(clang_str_to_vec(clang_getCursorSpelling(cur)),tokens)
150150
}
151151
}
152152
CXChildVisit_Continue
153153
});
154154
clang_disposeTranslationUnit(tu);
155155
};
156+
}
157+
158+
fn test_file(file: &str) -> bool {
159+
let mut idents=HashMap::new();
160+
let mut all_succeeded=true;
161+
file_visit_macros(file, fix_bug_9069(), |ident, tokens| all_succeeded&=test_definition(ident, &tokens, &mut idents));
156162
all_succeeded
157163
}
158164

165+
fn fix_bug_9069() -> bool {
166+
fn check_bug_9069() -> bool {
167+
let mut token_sets = vec![];
168+
file_visit_macros("tests/input/test_llvm_bug_9069.h", false, |ident, tokens| {
169+
assert_eq!(&ident, b"A");
170+
token_sets.push(tokens);
171+
});
172+
assert_eq!(token_sets.len(), 2);
173+
token_sets[0] != token_sets[1]
174+
}
175+
176+
use std::sync::{Once, ONCE_INIT};
177+
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
178+
179+
static CHECK_FIX: Once = ONCE_INIT;
180+
static FIX: AtomicBool = ATOMIC_BOOL_INIT;
181+
182+
CHECK_FIX.call_once(|| FIX.store(check_bug_9069(), Ordering::SeqCst));
183+
184+
FIX.load(Ordering::SeqCst)
185+
}
186+
159187
macro_rules! test_file {
160188
($f:ident) => {
161189
#[test] fn $f() {

tests/input/test_llvm_bug_9069.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// The following two definitions should yield the same list of tokens.
2+
// If https://bugs.llvm.org//show_bug.cgi?id=9069 is not fixed, they don't.
3+
#define A 1
4+
#define A 1

0 commit comments

Comments
 (0)