3

this seems to be more a C++ problem rather than a Clang problem...

I have to use C++ in order to write an OCLint (static code analyzer) rule.

I wish to compare two objects from the Clang library that have the type "SourceLocation".

This type provides informations about the location (basically line & column) of an object (statement, declaration etc.) in the code.

Basically, I would like to know if the statement A begins and ends before, or after a statement B.

In pseudo-code, that means I would like to get a boolean from :

( stmt_A->getLocBegin() < stmt_B->getLocBegin() ), for example. Of course, this does not compile because the "<" operator is not defined between two objects of type "SourceLocation".

I found a method in the Clang documentation, but, as I am no frequent user of C++, I don't find a way to use it, here is this method :

http://clang.llvm.org/doxygen/classclang_1_1BeforeThanCompare_3_01SourceLocation_01_4.html

clang::BeforeThanCompare<SourceLocation>::BeforeThanCompare (SourceManager &SM)


bool clang::BeforeThanCompare< SourceLocation >::operator()(SourceLocation LHS, SourceLocation RHS)  const [inline]

I do not know how to use SourceManager, or simply how to get this boolean above.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Marc-O
  • 701
  • 1
  • 5
  • 22
  • Not sure what your application is, but curious that you have a type for "SourceLocation". Once I created such a thing and called it `codeplace`--maybe this would be of interest: http://hoist.hostilefork.com/ – HostileFork says dont trust SE Jul 11 '14 at 06:31
  • @HostileFork It's `clang::SourceLocation`, it's a part of clang. – Angew is no longer proud of SO Jul 11 '14 at 06:32
  • @Agnew Thanks, I'll look it up, never heard of it! – HostileFork says dont trust SE Jul 11 '14 at 06:33
  • To call the comparison-performing `operator()` from `BeforeThanCompare`, you need to initialise it with a `SourceManager` object. As per its [docs](http://clang.llvm.org/doxygen/classclang_1_1SourceManager.html), that "class handles loading and caching of source files into memory." I assume you must have such an object in your project already, so just use it to construct the comparator. – Angew is no longer proud of SO Jul 11 '14 at 06:35
  • As a matter of fact, I don't have a SourceManager object in my code (and never in my previous codes), since I don't know how to use it. Do I just need to declare one ? Or is there some kind of procedure to link it with my SourceLocation ? – Marc-O Jul 11 '14 at 07:01
  • I finally found how to use properly everything, for those who might get the same problem : `SourceManager & loc_SM = _carrier->getSourceManager();` `BeforeThanCompare isBefore(loc_SM);` `SourceLocation stmt_A, stmt_B;` `bool A_before_B = isBefore(stmt_A,stmt_B);` – Marc-O Jul 11 '14 at 12:17
  • @Marc-O: So turn your comment into answer. – Jarod42 Jul 11 '14 at 19:46

1 Answers1

3

Here is the final code that shows how to use SourceManager in Clang library, and how to compare two SourceLocation :

// Declaration of a SourceManager
SourceManager & loc_SM = _carrier->getSourceManager();

// Declaration of an object BeforeThanCompare<SourceLocation>
BeforeThanCompare<SourceLocation> isBefore(loc_SM); SourceLocation stmt_A, stmt_B;

// Get whether stmt_A is before or after Stmt_B 
bool A_before_B = isBefore(stmt_A,stmt_B);
Marc-O
  • 701
  • 1
  • 5
  • 22