1 #include "source_location.hpp"
2 #include "utility.hpp"
3 
4 // // // // // // // //
5 //  SourceLocation   //
6 // // // // // // // //
SourceLocation(CXTranslationUnit & tu,const std::string & filepath,unsigned offset)7 clangmm::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset) {
8   CXFile file = clang_getFile(tu, filepath.c_str());
9   cx_location = clang_getLocationForOffset(tu, file, offset);
10 }
11 
SourceLocation(CXTranslationUnit & tu,const std::string & filepath,unsigned line,unsigned column)12 clangmm::SourceLocation::SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column) {
13   CXFile file = clang_getFile(tu, filepath.c_str());
14   cx_location = clang_getLocation(tu, file, line, column);
15 }
16 
get_path() const17 std::string clangmm::SourceLocation::get_path() const {
18   std::string path;
19   get_data(&path, nullptr, nullptr, nullptr);
20   return path;
21 }
get_offset() const22 clangmm::Offset clangmm::SourceLocation::get_offset() const {
23   unsigned line, index;
24   get_data(nullptr, &line, &index, nullptr);
25   return {line, index};
26 }
27 
get_data(std::string * path,unsigned * line,unsigned * column,unsigned * offset) const28 void clangmm::SourceLocation::get_data(std::string *path, unsigned *line, unsigned *column, unsigned *offset) const {
29   if(path == nullptr)
30     clang_getExpansionLocation(cx_location, NULL, line, column, offset);
31   else {
32     CXFile file;
33     clang_getExpansionLocation(cx_location, &file, line, column, offset);
34     if(file != NULL) {
35       *path = to_string(clang_getFileName(file));
36     }
37   }
38 }
39