[clang] [libclang/python] Fix bug in `SourceRange.__contains__`, add tests (PR #101802)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 12:24:08 PDT 2024


================
@@ -50,6 +50,18 @@ unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
           loc1.int_data == loc2.int_data);
 }
 
+unsigned clang_lessThanLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
+  const SourceLocation Loc1 = SourceLocation::getFromRawEncoding(loc1.int_data);
+  const SourceLocation Loc2 = SourceLocation::getFromRawEncoding(loc2.int_data);
+
+  const SourceManager &SM =
+      *static_cast<const SourceManager *>(loc1.ptr_data[0]);
+  if (!SM.isWrittenInSameFile(Loc1, Loc2))
----------------
Endilll wrote:

I think that in the following example:
```cpp
int a[] = {
#include "numbers.inc"
};
```
every source location in `numbers.inc` is contained within a source range that corresponds to curly braces, even though they come from different files. Hence I think `isWrittenInSameFile` check is not needed. You can also switch to `isBeforeInTranslationUnit`, but I expect it to come with a comment why we're not doing relying on `SourceLocation::operator<` to establish the ordering.
```

https://github.com/llvm/llvm-project/pull/101802


More information about the cfe-commits mailing list