Updated to use FileEntryRef to get name instead of FileEntry for clang18+ due the corresponding function is deprecated for the applicable clang versions. (#1792)

This commit is contained in:
Hong Chen 2024-10-15 10:37:03 -05:00 committed by GitHub
parent 1bdabadcbe
commit 48029fe031
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 42 deletions

View File

@ -395,8 +395,11 @@ std::set<std::string> PrintAttributes::getEmptyFiles() {
const clang::FileEntry * fe = (*fi).first ;
#if (LIBCLANG_MAJOR < 4) // TODO delete when RHEL 7 no longer supported
std::string header_file_name = fe->getName() ;
#else
#elif (LIBCLANG_MAJOR >= 4 && LIBCLANG_MAJOR < 18)
std::string header_file_name = fe->getName().str() ;
#else
const clang::FileEntryRef fer = fi->first ;
std::string header_file_name = fer.getName().str();
#endif
if ( visited_files.find(header_file_name) != visited_files.end() ) {

View File

@ -66,68 +66,69 @@ std::string trim(const std::string& str, const std::string& whitespace ) {
}
bool isInUserCode( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) {
clang::FileID fid = ci.getSourceManager().getFileID(sl) ;
bool ret = false ;
if ( ! fid.isInvalid() ) {
const clang::FileEntry * fe = ci.getSourceManager().getFileEntryForID(fid) ;
if ( fe != NULL ) {
#if (LIBCLANG_MAJOR < 4) // TODO delete when RHEL 7 no longer supported
char * resolved_path = almostRealPath( fe->getName() ) ;
#else
char * resolved_path = almostRealPath( fe->getName().str() ) ;
#endif
if ( resolved_path != NULL ) {
if ( hsd.isPathInUserDir(resolved_path)) {
ret = true ;
}
free(resolved_path) ;
}
char* resolved_path = getResolvedPath(ci, sl);
if ( resolved_path != NULL ) {
if ( hsd.isPathInUserDir(resolved_path)) {
ret = true ;
}
free(resolved_path) ;
}
return ret ;
}
bool isInUserOrTrickCode( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) {
clang::FileID fid = ci.getSourceManager().getFileID(sl) ;
bool ret = false ;
if ( ! fid.isInvalid() ) {
const clang::FileEntry * fe = ci.getSourceManager().getFileEntryForID(fid) ;
if ( fe != NULL ) {
#if (LIBCLANG_MAJOR < 4) // TODO delete when RHEL 7 no longer supported
char * resolved_path = almostRealPath( fe->getName() ) ;
#else
char * resolved_path = almostRealPath( fe->getName().str() ) ;
#endif
if ( resolved_path != NULL ) {
if ( hsd.isPathInUserOrTrickDir(resolved_path)) {
ret = true ;
}
free(resolved_path) ;
}
char* resolved_path = getResolvedPath(ci, sl);
if ( resolved_path != NULL ) {
if ( hsd.isPathInUserOrTrickDir(resolved_path)) {
ret = true ;
}
free(resolved_path) ;
}
return ret ;
}
std::string getFileName( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) {
clang::FileID fid = ci.getSourceManager().getFileID(sl) ;
std::string file_name;
char* resolved_path;
char* resolved_path = getResolvedPath(ci, sl);
if (resolved_path != NULL ) {
if (hsd.isPathInUserDir(resolved_path)) {
file_name.append(resolved_path);
}
free(resolved_path);
}
return file_name;
}
char * getResolvedPath(clang::CompilerInstance & ci , clang::SourceLocation sl) {
clang::FileID fid = ci.getSourceManager().getFileID(sl) ;
char* resolved_path = NULL;
if ( ! fid.isInvalid() ) {
const clang::FileEntry * fe = ci.getSourceManager().getFileEntryForID(fid) ;
if ( fe != NULL ) {
#if (LIBCLANG_MAJOR < 4) // TODO delete when RHEL 7 no longer supported
char * resolved_path = almostRealPath( fe->getName() ) ;
resolved_path = almostRealPath( fe->getName() ) ;
#elif (LIBCLANG_MAJOR >= 4 && LIBCLANG_MAJOR < 18)
resolved_path = almostRealPath( fe->getName().str() ) ;
#else
char * resolved_path = almostRealPath( fe->getName().str() ) ;
const clang::CustomizableOptional<clang::FileEntryRef> cfer = ci.getSourceManager().getFileEntryRefForID(fid) ;
if (cfer.has_value()) {
resolved_path = almostRealPath( cfer->getName().str() ) ;
}
#endif
if ( resolved_path != NULL and hsd.isPathInUserDir(resolved_path)) {
file_name.append(resolved_path);
}
free(resolved_path);
}
}
return file_name;
return resolved_path;
}
#include <iostream>

View File

@ -21,7 +21,7 @@ bool isInUserOrTrickCode( clang::CompilerInstance & ci , clang::SourceLocation s
std::string getFileName( clang::CompilerInstance & ci , clang::SourceLocation sl , HeaderSearchDirs & hsd ) ;
char * almostRealPath( const std::string& in_path ) ;
char * almostRealPath( const char * in_path ) ;
char * getResolvedPath(clang::CompilerInstance & ci , clang::SourceLocation sl);
std::string color(const Color& color, const std::string& text);
std::string bold(const std::string& text);
std::string underline(const std::string& text);

View File

@ -312,7 +312,7 @@ int main(int argc, char * argv[]) {
#if (LIBCLANG_MAJOR >= 10 && LIBCLANG_MAJOR < 18)
const clang::FileEntry* fileEntry = ci.getFileManager().getFile(inputFilePath).get();
#elif (LIBCLANG_MAJOR >= 18)
clang::FileEntryRef fileEntryRef = llvm::cantFail(ci.getFileManager().getFileRef(inputFilePath));
const clang::FileEntryRef fileEntryRef = llvm::cantFail(ci.getFileManager().getFileRef(inputFilePath));
#else
const clang::FileEntry* fileEntry = ci.getFileManager().getFile(inputFilePath);
#endif