Skip to content

Commit

Permalink
Added (slightly modified) copy of libClangSharp.
Browse files Browse the repository at this point in the history
  • Loading branch information
PathogenDavid committed Aug 29, 2021
1 parent 9937c0a commit cbe6732
Show file tree
Hide file tree
Showing 19 changed files with 982 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This fork extends the functionality of libclang. Amonth other things, it primari

All functionality provided by this fork can be found in [`PathogenExtensions.cpp`](clang/tools/libclang/PathogenExtensions.cpp).

This fork also includes a (slightly modified) copy of libClangSharp, see [clang/tools/libclang/libClangSharp](clang/tools/libclang/libClangSharp/Readme.md) for details.

Essentially it exposes information provided by `ASTRecordLayout` and `MicrosoftVTableContext`/`ItaniumVTableContext`.
Both sets of information are intended to be ABI-agnostic.

Expand Down
18 changes: 18 additions & 0 deletions clang/tools/libclang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ set(SOURCES
Rewrite.cpp
PathogenExtensions.cpp

libClangSharp/CIndexDiagnostic.cpp
libClangSharp/ClangSharp.cpp
libClangSharp/CXCursor.cpp
libClangSharp/CXLoadedDiagnostic.cpp
libClangSharp/CXSourceLocation.cpp
libClangSharp/CXString.cpp
libClangSharp/CXTranslationUnit.cpp
libClangSharp/CXType.cpp

ADDITIONAL_HEADERS
CIndexDiagnostic.h
CIndexer.h
Expand All @@ -34,6 +43,15 @@ set(SOURCES
CXType.h
Index_Internal.h
../../include/clang-c/Index.h

libClangSharp/CIndexDiagnostic.h
libClangSharp/ClangSharp.h
libClangSharp/CXCursor.h
libClangSharp/CXLoadedDiagnostic.h
libClangSharp/CXSourceLocation.h
libClangSharp/CXString.h
libClangSharp/CXTranslationUnit.h
libClangSharp/CXType.h
)

set(LIBS
Expand Down
9 changes: 9 additions & 0 deletions clang/tools/libclang/libClangSharp/CIndexDiagnostic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#include "libClangSharp/CIndexDiagnostic.h"

namespace clang {
}
105 changes: 105 additions & 0 deletions clang/tools/libclang/libClangSharp/CIndexDiagnostic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#ifndef LIBCLANGSHARP_CINDEXDIAGNOSTIC_H
#define LIBCLANGSHARP_CINDEXDIAGNOSTIC_H

#include "CXString.h"
#include "clang-c/Index.h"

#include <cassert>
#include <memory>
#include <utility>
#include <vector>

namespace clang {
class CXDiagnosticImpl;

class CXDiagnosticSetImpl {
std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics;
const bool IsExternallyManaged;
public:
CXDiagnosticSetImpl(bool isManaged = false)
: IsExternallyManaged(isManaged) { }

virtual ~CXDiagnosticSetImpl();

size_t getNumDiagnostics() const {
return Diagnostics.size();
}

CXDiagnosticImpl* getDiagnostic(unsigned i) const {
assert(i < getNumDiagnostics());
return Diagnostics[i].get();
}

void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D);

bool empty() const {
return Diagnostics.empty();
}

bool isExternallyManaged() const { return IsExternallyManaged; }
};

class CXDiagnosticImpl {
public:
enum Kind {
StoredDiagnosticKind, LoadedDiagnosticKind,
CustomNoteDiagnosticKind
};

virtual ~CXDiagnosticImpl();

/// Return the severity of the diagnostic.
virtual CXDiagnosticSeverity getSeverity() const = 0;

/// Return the location of the diagnostic.
virtual CXSourceLocation getLocation() const = 0;

/// Return the spelling of the diagnostic.
virtual CXString getSpelling() const = 0;

/// Return the text for the diagnostic option.
virtual CXString getDiagnosticOption(CXString* Disable) const = 0;

/// Return the category of the diagnostic.
virtual unsigned getCategory() const = 0;

/// Return the category string of the diagnostic.
virtual CXString getCategoryText() const = 0;

/// Return the number of source ranges for the diagnostic.
virtual unsigned getNumRanges() const = 0;

/// Return the source ranges for the diagnostic.
virtual CXSourceRange getRange(unsigned Range) const = 0;

/// Return the number of FixIts.
virtual unsigned getNumFixIts() const = 0;

/// Return the FixIt information (source range and inserted text).
virtual CXString getFixIt(unsigned FixIt, CXSourceRange* ReplacementRange) const = 0;

Kind getKind() const { return K; }

CXDiagnosticSetImpl& getChildDiagnostics() {
return ChildDiags;
}

protected:
CXDiagnosticImpl(Kind k) : K(k) {}
CXDiagnosticSetImpl ChildDiags;

void append(std::unique_ptr<CXDiagnosticImpl> D) {
ChildDiags.appendDiagnostic(std::move(D));
}

private:
Kind K;
};
}

#endif
18 changes: 18 additions & 0 deletions clang/tools/libclang/libClangSharp/CXCursor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#include "libClangSharp/ClangSharp.h"
#include "libClangSharp/CXCursor.h"
#include "libClangSharp/CXTranslationUnit.h"

using namespace clang::cxtu;

namespace clang {
namespace cxcursor {
MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
return C;
}
}
}
116 changes: 116 additions & 0 deletions clang/tools/libclang/libClangSharp/CXCursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#ifndef LIBCLANGSHARP_CXCURSOR_H
#define LIBCLANGSHARP_CXCURSOR_H

#include <clang/AST/Attr.h>
#include <clang/AST/Decl.h>
#include <clang/AST/Expr.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/TemplateName.h>
#include <clang/Frontend/ASTUnit.h>
#include "clang-c/Index.h"

#include <llvm/ADT/PointerUnion.h>

namespace clang {
namespace cxcursor {
typedef llvm::PointerUnion<const OverloadExpr*, const Decl*, OverloadedTemplateStorage* > OverloadedDeclRefStorage;

/// Wraps a macro expansion cursor and provides a common interface
/// for a normal macro expansion cursor or a "pseudo" one.
///
/// "Pseudo" macro expansion cursors (essentially a macro definition along with
/// a source location) are created in special cases, for example they can be
/// created for identifiers inside macro definitions, if these identifiers are
/// macro names.
class MacroExpansionCursor {
CXCursor C;

bool isPseudo() const { return C.data[1] != nullptr; }
const MacroDefinitionRecord* getAsMacroDefinition() const {
assert(isPseudo());
return static_cast<const MacroDefinitionRecord*>(C.data[0]);
}
const MacroExpansion* getAsMacroExpansion() const {
assert(!isPseudo());
return static_cast<const MacroExpansion*>(C.data[0]);
}
SourceLocation getPseudoLoc() const {
assert(isPseudo());
return SourceLocation::getFromPtrEncoding(C.data[1]);
}

public:
MacroExpansionCursor(CXCursor C) : C(C) {
assert(C.kind == CXCursor_MacroExpansion);
}

const IdentifierInfo* getName() const;
const MacroDefinitionRecord* getDefinition() const;
SourceRange getSourceRange() const;
};

ASTUnit* getCursorASTUnit(CXCursor Cursor);
ASTContext& getCursorContext(CXCursor Cursor);
CXTranslationUnit getCursorTU(CXCursor Cursor);

const Attr* getCursorAttr(CXCursor Cursor);
const Decl* getCursorDecl(CXCursor Cursor);
const Expr* getCursorExpr(CXCursor Cursor);
const Stmt* getCursorStmt(CXCursor Cursor);

/// Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
const CXXBaseSpecifier* getCursorCXXBaseSpecifier(CXCursor C);

/// Unpack a given inclusion directive cursor to retrieve its source range.
const InclusionDirective* getCursorInclusionDirective(CXCursor C);

/// Unpack a given macro definition cursor to retrieve its source range.
const MacroDefinitionRecord* getCursorMacroDefinition(CXCursor C);

/// Unpack a given macro expansion cursor to retrieve its info.
MacroExpansionCursor getCursorMacroExpansion(CXCursor C);

/// Unpack a given preprocessing directive to retrieve its source range.
SourceRange getCursorPreprocessingDirective(CXCursor C);

/// Unpack a label reference into the label statement it refers to and the location of the reference.
std::pair<const LabelStmt*, SourceLocation> getCursorLabelRef(CXCursor C);

/// Unpack a MemberRef cursor into the field it references and the location where the reference occurred.
std::pair<const FieldDecl*, SourceLocation> getCursorMemberRef(CXCursor C);

/// Unpack a NamespaceRef cursor into the namespace or namespace alias it references and the location where the reference occurred.
std::pair<const NamedDecl*, SourceLocation> getCursorNamespaceRef(CXCursor C);

/// Unpack an ObjCClassRef cursor into the class it references and optionally the location where the reference occurred.
std::pair<const ObjCInterfaceDecl*, SourceLocation> getCursorObjCClassRef(CXCursor C);

/// Unpack an ObjCProtocolRef cursor into the protocol it references and optionally the location where the reference occurred.
std::pair<const ObjCProtocolDecl*, SourceLocation> getCursorObjCProtocolRef(CXCursor C);

/// Unpack an ObjCSuperClassRef cursor into the interface it references and optionally the location where the reference occurred.
std::pair<const ObjCInterfaceDecl*, SourceLocation> getCursorObjCSuperClassRef(CXCursor C);

/// Unpack an overloaded declaration reference into an expression, declaration, or template name along with the source location.
std::pair<OverloadedDeclRefStorage, SourceLocation> getCursorOverloadedDeclRef(CXCursor C);

/// Unpack a TemplateRef cursor into the template it references and the location where the reference occurred.
std::pair<const TemplateDecl*, SourceLocation> getCursorTemplateRef(CXCursor C);

/// Unpack a TypeRef cursor into the class it references and optionally the location where the reference occurred.
std::pair<const TypeDecl*, SourceLocation> getCursorTypeRef(CXCursor C);

/// Unpack a VariableRef cursor into the variable it references and the location where the where the reference occurred.
std::pair<const VarDecl*, SourceLocation> getCursorVariableRef(CXCursor C);

// FIXME: Remove once we can model DeclGroups and their appropriate ranges properly in the ASTs.
bool isFirstInDeclGroup(CXCursor C);
}
}

#endif
21 changes: 21 additions & 0 deletions clang/tools/libclang/libClangSharp/CXLoadedDiagnostic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#include "libClangSharp/CXLoadedDiagnostic.h"
#include "libClangSharp/CXString.h"

#include <clang/Frontend/SerializedDiagnostics.h>
#include <llvm/ADT/Twine.h>

namespace clang {
static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location* DLoc) {
// The lowest bit of ptr_data[0] is always set to 1 to indicate this
// is a persistent diagnostic.
uintptr_t V = (uintptr_t)DLoc;
V |= 0x1;
CXSourceLocation Loc = { { (void*)V, nullptr }, 0 };
return Loc;
}
}
78 changes: 78 additions & 0 deletions clang/tools/libclang/libClangSharp/CXLoadedDiagnostic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.

// Ported from https://github.com/llvm/llvm-project/tree/llvmorg-10.0.0/clang/tools/libclang
// Original source is Copyright (c) the LLVM Project and Contributors. Licensed under the Apache License v2.0 with LLVM Exceptions. See NOTICE.txt in the project root for license information.

#ifndef LIBCLANGSHARP_CXLOADEDDIAGNOSTIC_H
#define LIBCLANGSHARP_CXLOADEDDIAGNOSTIC_H

#include "libClangSharp/CIndexDiagnostic.h"
#include <llvm/ADT/StringRef.h>

namespace clang {
class CXLoadedDiagnostic : public CXDiagnosticImpl {
public:
CXLoadedDiagnostic()
: CXDiagnosticImpl(LoadedDiagnosticKind), severity(0), category(0) { }

~CXLoadedDiagnostic() override;

/// Return the severity of the diagnostic.
CXDiagnosticSeverity getSeverity() const override;

/// Return the location of the diagnostic.
CXSourceLocation getLocation() const override;

/// Return the spelling of the diagnostic.
CXString getSpelling() const override;

/// Return the text for the diagnostic option.
CXString getDiagnosticOption(CXString* Disable) const override;

/// Return the category of the diagnostic.
unsigned getCategory() const override;

/// Return the category string of the diagnostic.
CXString getCategoryText() const override;

/// Return the number of source ranges for the diagnostic.
unsigned getNumRanges() const override;

/// Return the source ranges for the diagnostic.
CXSourceRange getRange(unsigned Range) const override;

/// Return the number of FixIts.
unsigned getNumFixIts() const override;

/// Return the FixIt information (source range and inserted text).
CXString getFixIt(unsigned FixIt, CXSourceRange* ReplacementRange) const override;

static bool classof(const CXDiagnosticImpl* D) {
return D->getKind() == LoadedDiagnosticKind;
}

/// Decode the CXSourceLocation into file, line, column, and offset.
static void decodeLocation(CXSourceLocation location, CXFile* file, unsigned* line, unsigned* column, unsigned* offset);

struct Location {
CXFile file;
unsigned line;
unsigned column;
unsigned offset;

Location() : line(0), column(0), offset(0) { }
};

Location DiagLoc;

std::vector<CXSourceRange> Ranges;
std::vector<std::pair<CXSourceRange, const char*>> FixIts;
const char* Spelling;
llvm::StringRef DiagOption;
llvm::StringRef CategoryText;
unsigned severity;
unsigned category;
};
}

#endif
Loading

0 comments on commit cbe6732

Please sign in to comment.