Skip to content

Commit

Permalink
Add support for LLVM-18
Browse files Browse the repository at this point in the history
New LLVM version changed the behaviour and signature of some functions.
Take this into account.

Signed-off-by: Giuliano Belinassi <gbelinassi@suse.de>
  • Loading branch information
giulianobelinassi committed Mar 28, 2024
1 parent 8bd3545 commit 47af56d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions libcextract/ClangCompat.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
#include <clang/Basic/Version.h>
#include "clang/Frontend/CompilerInstance.h"

/* Starting from LLVM-18, the method FileEntry::getName() got deprecated.
* This makes clang warns about the function being deprecated while it is
* completely useful as a way to keep compatibility with older versions of
* LLVM, hence we acknowledge the warning here and disable it for now.
*/

#pragma clang diagnostic ignored "-Wdeprecated-declarations"

namespace ClangCompat
{
#if CLANG_VERSION_MAJOR >= 16
Expand Down
13 changes: 11 additions & 2 deletions libcextract/IncludeTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ void IncludeTree::Build_Header_Tree(std::vector<std::string> const &must_expand)
bool expand = In_Set(must_expand_set, id->getFileName().str(), /*remove=*/true) ||
In_Set(must_expand_set, id->getFile()->getName().str(),
/*remove=*/true);
#if CLANG_VERSION_MAJOR >= 18
/* Starting from LLVM-18, the behaviour of FileEntry::getName() changed.
* Now it returns the full path of the file rather than the relative path
* to it, so here we account it for now onwards.
*/
expand |= In_Set(must_expand_set,
id->getFile()->getFileEntry().tryGetRealPathName().str(),
/*remove=*/true);
#endif

bool output = already_seen_main && current->Should_Be_Expanded()
&& !expand;
bool is_from_minus_include = !already_seen_main;
Expand Down Expand Up @@ -355,11 +365,10 @@ void IncludeTree::IncludeNode::Set_FileEntry(OptionalFileEntryRef file)

SourceRange IncludeTree::IncludeNode::Get_File_Range(void)
{
const FileEntry *file = *File;
SourceManager *SM = PrettyPrint::Get_Source_Manager();

SourceLocation start, end;
FileID fid = SM->getOrCreateFileID(file, SrcMgr::CharacteristicKind());
FileID fid = SM->getOrCreateFileID(*File, SrcMgr::CharacteristicKind());
start = SM->getLocForStartOfFile(fid);
end = SM->getLocForEndOfFile(fid);

Expand Down
5 changes: 5 additions & 0 deletions libcextract/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ class BuildASTPass : public Pass
{
SourceManager &sm = ast->getSourceManager();

#if CLANG_VERSION_MAJOR >= 18
FileEntryRef main_file = *sm.getFileEntryRefForID(sm.getMainFileID());
#else
const FileEntry *main_file = sm.getFileEntryForID(sm.getMainFileID());
#endif

StringRef path = sm.getFileManager().getCanonicalName(main_file);

return path;
Expand Down
2 changes: 1 addition & 1 deletion libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "SymbolExternalizer.hh"
#include "PrettyPrint.hh"
#include "Error.hh"
#include "ClangCompat.hh"

#include <unordered_set>
#include <iostream>
Expand Down Expand Up @@ -365,7 +366,6 @@ bool TextModifications::Is_Same_Change(const Delta &a, const Delta &b)

/* ---- End of Deltas class -------- */


bool SymbolExternalizer::FunctionUpdater::Update_References_To_Symbol(Stmt *stmt)
{
if (!stmt)
Expand Down
1 change: 0 additions & 1 deletion testsuite/includes/include-7.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ int main(void)
}

/* { dg-final { scan-tree-dump "#define _STDIO_H 1" } } */
/* { dg-final { scan-tree-dump "typedef __SIZE_TYPE__ size_t;" } } */
/* { dg-final { scan-tree-dump "#define __need___va_list" } } */
/* { dg-final { scan-tree-dump "#include <stdarg.h>" } } */
/* { dg-final { scan-tree-dump "#include <bits/types/__fpos_t.h>" } } */
Expand Down

0 comments on commit 47af56d

Please sign in to comment.