Use expansion location when looking for the location of a decl that comes from a macro invocation
PiperOrigin-RevId: 425691134
diff --git a/rs_bindings_from_cc/importer.cc b/rs_bindings_from_cc/importer.cc
index b7e02f5..5c3e473 100644
--- a/rs_bindings_from_cc/importer.cc
+++ b/rs_bindings_from_cc/importer.cc
@@ -453,7 +453,6 @@
BlazeLabel Importer::GetOwningTarget(const clang::Decl* decl) const {
clang::SourceManager& source_manager = ctx_.getSourceManager();
auto source_location = decl->getLocation();
- auto id = source_manager.getFileID(source_location);
// If the header this decl comes from is not associated with a target we
// consider it a textual header. In that case we go up the include stack
@@ -464,6 +463,10 @@
// "//:virtual_clang_resource_dir_target" for system headers.
while (source_location.isValid() &&
!source_manager.isInSystemHeader(source_location)) {
+ if (source_location.isMacroID()) {
+ source_location = source_manager.getExpansionLoc(source_location);
+ }
+ auto id = source_manager.getFileID(source_location);
llvm::Optional<llvm::StringRef> filename =
source_manager.getNonBuiltinFilenameForID(id);
if (!filename) {
@@ -477,7 +480,6 @@
return *target;
}
source_location = source_manager.getIncludeLoc(id);
- id = source_manager.getFileID(source_location);
}
return BlazeLabel("//:virtual_clang_resource_dir_target");
diff --git a/rs_bindings_from_cc/test/macro_locations/defines_macro.h b/rs_bindings_from_cc/test/macro_locations/defines_macro.h
new file mode 100644
index 0000000..3744dc0
--- /dev/null
+++ b/rs_bindings_from_cc/test/macro_locations/defines_macro.h
@@ -0,0 +1,13 @@
+// Part of the Crubit project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#ifndef CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_H_
+#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_H_
+
+#define DEFINE_STRUCT(type) \
+ struct StructFromMacro { \
+ type val; \
+ };
+
+#endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_H_
diff --git a/rs_bindings_from_cc/test/macro_locations/defines_macro_that_uses_macro.h b/rs_bindings_from_cc/test/macro_locations/defines_macro_that_uses_macro.h
new file mode 100644
index 0000000..8105511
--- /dev/null
+++ b/rs_bindings_from_cc/test/macro_locations/defines_macro_that_uses_macro.h
@@ -0,0 +1,14 @@
+// Part of the Crubit project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#ifndef CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_THAT_USES_MACRO_H_
+#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_THAT_USES_MACRO_H_
+
+#include "rs_bindings_from_cc/test/macro_locations/defines_macro.h"
+
+#define DEFINE_STRUCT_AND_FUNCTION(type) \
+ DEFINE_STRUCT(type) \
+ inline type functionFromMacro(type x) { return x; }
+
+#endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_DEFINES_MACRO_THAT_USES_MACRO_H_
diff --git a/rs_bindings_from_cc/test/macro_locations/uses_macro.h b/rs_bindings_from_cc/test/macro_locations/uses_macro.h
new file mode 100644
index 0000000..58a24de
--- /dev/null
+++ b/rs_bindings_from_cc/test/macro_locations/uses_macro.h
@@ -0,0 +1,12 @@
+// Part of the Crubit project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#ifndef CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_USES_MACRO_H_
+#define CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_USES_MACRO_H_
+
+#include "rs_bindings_from_cc/test/macro_locations/defines_macro_that_uses_macro.h"
+
+DEFINE_STRUCT_AND_FUNCTION(int)
+
+#endif // CRUBIT_RS_BINDINGS_FROM_CC_TEST_MACRO_LOCATIONS_USES_MACRO_H_
diff --git a/rs_bindings_from_cc/test/macro_locations/uses_struct_and_function_from_macro.rs b/rs_bindings_from_cc/test/macro_locations/uses_struct_and_function_from_macro.rs
new file mode 100644
index 0000000..bdf3d7a
--- /dev/null
+++ b/rs_bindings_from_cc/test/macro_locations/uses_struct_and_function_from_macro.rs
@@ -0,0 +1,13 @@
+// Part of the Crubit project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#[cfg(test)]
+mod tests {
+
+ #[test]
+ fn test_uses_struct_and_function_from_macro() {
+ let my_struct = uses_macro::StructFromMacro { val: 3 };
+ assert_eq!(my_struct.val, uses_macro::functionFromMacro(3));
+ }
+}