forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Proposed syntax APIs #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef2f861
[Testing] Move clangd::Annotations to llvm testing support
ilya-biryukov 30e0efa
[Lex] Allow to consume tokens while preprocessing
ilya-biryukov ec672f3
[Syntax] Introduce TokenBuffer, start clangToolingSyntax library
ilya-biryukov 299bd89
[Syntax] Add a findMacroCall helper
ilya-biryukov c1e85c6
[AST] Make CXXBindTemporaryExpr final. NFC
ilya-biryukov 02f62fe
[AST] Treat inherited default argument as implicit code
ilya-biryukov 1bd775e
[Syntax] Introduce mutable syntax trees
ilya-biryukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2090,6 +2090,9 @@ DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, { | |
DEF_TRAVERSE_DECL(ParmVarDecl, { | ||
TRY_TO(TraverseVarHelper(D)); | ||
|
||
if (!shouldVisitImplicitCode() && D->hasInheritedDefaultArg()) | ||
return true; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, I think you can submit this today, as long as you add a test. |
||
if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() && | ||
!D->hasUnparsedDefaultArg()) | ||
TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg())); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//===- Corpus.h - memory arena and bookkeeping for syntax trees --- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef LLVM_CLANG_TOOLING_SYNTAX_CORPUS_H | ||
#define LLVM_CLANG_TOOLING_SYNTAX_CORPUS_H | ||
|
||
#include "clang/Basic/LangOptions.h" | ||
#include "clang/Basic/SourceManager.h" | ||
#include "clang/Tooling/Syntax/TokenBuffer.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/Support/Allocator.h" | ||
|
||
namespace clang { | ||
namespace syntax { | ||
/// A corpus is a memory arena for a set of syntax trees. In addition, it also | ||
/// tracks the underlying token buffers, manager, etc. | ||
class Corpus { | ||
public: | ||
Corpus(SourceManager &SourceMgr, const LangOptions &LangOpts, | ||
TokenBuffer MainFile); | ||
|
||
const SourceManager &sourceManager() const { return SourceMgr; } | ||
const LangOptions &langOptions() const { return LangOpts; } | ||
|
||
/// Construct a new syntax node of a specified kind. The memory for a node is | ||
/// owned by the corpus and will be freed when the corpus is destroyed. | ||
template <class TNode, class... Args> TNode *construct(Args &&... As) { | ||
static_assert(std::is_trivially_destructible<TNode>::value, | ||
"nodes should be trivially destructible"); | ||
return new (Allocator) TNode(std::forward<Args>(As)...); | ||
} | ||
|
||
const TokenBuffer *findBuffer(FileID FID) const; | ||
const TokenBuffer *findBuffer(SourceLocation Loc) const; | ||
|
||
const TokenBuffer &mainFile() const; | ||
|
||
llvm::BumpPtrAllocator &allocator() { return Allocator; } | ||
|
||
/// Get a text for a continuous range of tokens. | ||
/// EXPECTS: Tokens are a part of some tokenized buffer from \p Buffers. | ||
llvm::StringRef getText(llvm::ArrayRef<syntax::Token> Tokens); | ||
|
||
/// Tokenize the passed buffer. This function runs a lexer in raw mode, i.e. | ||
/// the result won't contain any macro expansions, etc. | ||
std::pair<FileID, const TokenBuffer &> | ||
tokenizeBuffer(std::unique_ptr<llvm::MemoryBuffer> Buffer); | ||
|
||
private: | ||
SourceManager &SourceMgr; | ||
const LangOptions &LangOpts; | ||
llvm::DenseMap<FileID, TokenBuffer> Buffers; | ||
/// Keeps all the allocated nodes. | ||
llvm::BumpPtrAllocator Allocator; | ||
}; | ||
|
||
} // namespace syntax | ||
} // namespace clang | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//===- NodeList.h ---------------------------------------------*- C++ -*-=====// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLING_SYNTAX_NODELIST_H | ||
#define LLVM_CLANG_TOOLING_SYNTAX_NODELIST_H | ||
|
||
#include "llvm/Support/Allocator.h" | ||
#include <algorithm> | ||
#include <type_traits> | ||
|
||
namespace clang { | ||
namespace syntax { | ||
namespace detail { | ||
/// A vector that requests memory from BumpPtrAllocator and has a trivial | ||
/// destructor. Syntax tree nodes use it to store children. | ||
template <class T> class BumpAllocVector { | ||
// Make sure the elements are allowed to drop destructors. | ||
static_assert(std::is_trivially_destructible<T>::value, | ||
"T must be trivially destructible"); | ||
// Implementation for trivially-copyable types is much simpler. | ||
static_assert(std::is_trivially_copyable<T>::value, | ||
"T must be trivially copyable."); | ||
|
||
public: | ||
T *begin() { return Begin; } | ||
T *end() { return End; } | ||
|
||
const T *begin() const { return Begin; } | ||
const T *end() const { return End; } | ||
|
||
size_t size() const { return End - Begin; } | ||
bool empty() const { return begin() == end(); } | ||
|
||
void push_back(llvm::BumpPtrAllocator &A, T Element) { | ||
if (StorageEnd == End) | ||
Grow(A); | ||
*End = Element; | ||
++End; | ||
} | ||
|
||
void erase(T *Begin, T *End) { | ||
std::ptrdiff_t ErasedSize = End - Begin; | ||
for (auto *It = End; It != this->End; ++It) { | ||
*Begin = It; | ||
++Begin; | ||
} | ||
End -= ErasedSize; | ||
} | ||
|
||
private: | ||
void Grow(llvm::BumpPtrAllocator &A) { | ||
size_t Size = End - Begin; | ||
|
||
size_t NewCapacity = 2 * (StorageEnd - Begin); | ||
if (NewCapacity == 0) | ||
NewCapacity = 1; | ||
|
||
T *NewStorage = A.Allocate<T>(NewCapacity); | ||
std::copy(Begin, End, NewStorage); | ||
|
||
A.Deallocate(Begin, StorageEnd - Begin); | ||
|
||
Begin = NewStorage; | ||
End = NewStorage + Size; | ||
StorageEnd = NewStorage + NewCapacity; | ||
} | ||
|
||
private: | ||
T *Begin = nullptr; | ||
T *End = nullptr; | ||
T *StorageEnd = nullptr; | ||
}; | ||
} // namespace detail | ||
|
||
class Node; | ||
/// Like vector<Node*>, but allocates all the memory in the BumpPtrAllocator. | ||
/// Can be dropped without running the destructor. | ||
using NodeList = detail::BumpAllocVector<Node *>; | ||
} // namespace syntax | ||
} // namespace clang | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can totally submit this change today.