Skip to content

Commit

Permalink
Mark two more methods as nullable. Add IsBuiltin and IsCommandLine to…
Browse files Browse the repository at this point in the history
… DefineMacroDirective.
  • Loading branch information
pgoodman committed Aug 19, 2024
1 parent 6d97c66 commit 0251b26
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 4 deletions.
8 changes: 8 additions & 0 deletions bin/BootstrapTypes/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,14 @@ std::map<std::pair<std::string, std::string>, std::string> kConditionalNullptr{
{{"CXXRecordDecl", "NeedsOverloadResolutionForDestructor"}, SELF_IS_DEFINITION},
{{"CXXRecordDecl", "NeedsOverloadResolutionForMoveAssignment"}, SELF_IS_DEFINITION},
{{"CXXRecordDecl", "NeedsOverloadResolutionForMoveConstructor"}, SELF_IS_DEFINITION},
{{"CXXRecordDecl", "DeviceLambdaManglingNumber"},
" if (!self.isLambda()) {\n"
" return std::nullopt;\n"
" }\n"},
{{"CXXRecordDecl", "LambdaIndexInContext"},
" if (!self.isLambda()) {\n"
" return std::nullopt;\n"
" }\n"},

{{"NamedDecl", "ObjCFStringFormattingFamily"},
" if (!self.getIdentifier()) {\n"
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/src/Macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ PASTA_FOR_EACH_MACRO_IMPL(PASTA_IGNORE,
.def_prop_ro("num_explicit_parameters", &DefineMacroDirective::NumExplicitParameters)
.def_prop_ro("is_variadic", &DefineMacroDirective::IsVariadic)
.def_prop_ro("is_function_like", &DefineMacroDirective::IsFunctionLike)
.def_prop_ro("is_builtin", &DefineMacroDirective::IsBuiltin)
.def_prop_ro("is_command_linw", &DefineMacroDirective::IsCommandLine)
.def_prop_ro("parameters", &DefineMacroDirective::Parameters);

nb::class_<MacroArgument, Macro>(m, "MacroArgument")
Expand Down
4 changes: 2 additions & 2 deletions include/pasta/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2996,15 +2996,15 @@ class CXXRecordDecl : public RecordDecl {
std::optional<::pasta::FunctionTemplateDecl> DependentLambdaCallOperator(void) const;
std::optional<::pasta::ClassTemplateDecl> DescribedClassTemplate(void) const;
std::optional<::pasta::CXXDestructorDecl> Destructor(void) const;
uint32_t DeviceLambdaManglingNumber(void) const;
std::optional<uint32_t> DeviceLambdaManglingNumber(void) const;
std::optional<::pasta::TemplateParameterList> GenericLambdaTemplateParameterList(void) const;
std::optional<::pasta::CXXRecordDecl> InstantiatedFromMemberClass(void) const;
std::optional<::pasta::CXXMethodDecl> LambdaCallOperator(void) const;
std::optional<enum LambdaCaptureDefault> LambdaCaptureDefault(void) const;
std::optional<::pasta::Decl> LambdaContextDeclaration(void) const;
uint32_t LambdaDependencyKind(void) const;
std::optional<std::vector<::pasta::NamedDecl>> LambdaExplicitTemplateParameters(void) const;
uint32_t LambdaIndexInContext(void) const;
std::optional<uint32_t> LambdaIndexInContext(void) const;
std::optional<uint32_t> LambdaManglingNumber(void) const;
// LambdaNumbering: (clang::CXXRecordDecl::LambdaNumbering)
std::optional<::pasta::CXXMethodDecl> LambdaStaticInvoker(void) const;
Expand Down
7 changes: 7 additions & 0 deletions include/pasta/AST/Macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ class DefineMacroDirective final : public MacroDirective {
// arguments when used.
bool IsFunctionLike(void) const noexcept;

// Is this a builtin macro? Builtin macros are defined in the compiler macro
// preamble.
bool IsBuiltin(void) const noexcept;

// Is this defined at the command-line?
bool IsCommandLine(void) const noexcept;

// Parameters of this macro definition.
MacroRange Parameters(void) const noexcept;
};
Expand Down
10 changes: 8 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8754,8 +8754,11 @@ std::optional<::pasta::CXXDestructorDecl> CXXRecordDecl::Destructor(void) const
}
}

uint32_t CXXRecordDecl::DeviceLambdaManglingNumber(void) const {
std::optional<uint32_t> CXXRecordDecl::DeviceLambdaManglingNumber(void) const {
auto &self = *const_cast<clang::CXXRecordDecl *>(u.CXXRecordDecl);
if (!self.isLambda()) {
return std::nullopt;
}
decltype(auto) val = self.getDeviceLambdaManglingNumber();
return val;
}
Expand Down Expand Up @@ -8841,8 +8844,11 @@ std::optional<std::vector<::pasta::NamedDecl>> CXXRecordDecl::LambdaExplicitTemp
return ret;
}

uint32_t CXXRecordDecl::LambdaIndexInContext(void) const {
std::optional<uint32_t> CXXRecordDecl::LambdaIndexInContext(void) const {
auto &self = *const_cast<clang::CXXRecordDecl *>(u.CXXRecordDecl);
if (!self.isLambda()) {
return std::nullopt;
}
decltype(auto) val = self.getLambdaIndexInContext();
return val;
}
Expand Down
31 changes: 31 additions & 0 deletions lib/AST/Macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Lex/Lexer.h>
#include <clang/Lex/MacroInfo.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/Token.h>
#pragma GCC diagnostic pop

Expand Down Expand Up @@ -897,6 +898,36 @@ bool DefineMacroDirective::IsFunctionLike(void) const noexcept {
return 0u;
}

// Is this a builtin macro? Builtin macros are defined in the compiler macro
// preamble.
bool DefineMacroDirective::IsBuiltin(void) const noexcept {
Node node = *reinterpret_cast<const Node *>(impl);
MacroNodeImpl *node_impl = std::get<MacroNodeImpl *>(node);
MacroDirectiveImpl *dir_impl = dynamic_cast<MacroDirectiveImpl *>(node_impl);
if (!dir_impl->defined_macro) {
assert(false);
return false;
}

if (dir_impl->is_command_line) {
return false;
}

const auto &ci = *ast->ci.get();
const auto &sm = ci.getSourceManager();
auto file_id = ast->orig_source_pp->getPredefinesFileID();
auto def_loc = dir_impl->defined_macro->getDefinitionLoc();
return file_id == sm.getDecomposedLoc(def_loc).first;
}

// Is this defined at the command-line?
bool DefineMacroDirective::IsCommandLine(void) const noexcept {
Node node = *reinterpret_cast<const Node *>(impl);
MacroNodeImpl *node_impl = std::get<MacroNodeImpl *>(node);
MacroDirectiveImpl *dir_impl = dynamic_cast<MacroDirectiveImpl *>(node_impl);
return dir_impl->is_command_line;
}

// Does this definition accept a variable number of arguments?
bool DefineMacroDirective::IsVariadic(void) const noexcept {
Node node = *reinterpret_cast<const Node *>(impl);
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/Macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ class MacroDirectiveImpl final : public MacroNodeImpl {
// missing tokens when we come accross an EOD.
bool collected_missing_tokens_on_eod{false};

// Is this macro defined on the command-line?
bool is_command_line{false};

// Return the offset of the marker token.
DerivedTokenIndex marker_token_offset{~0u};
};
Expand Down
17 changes: 17 additions & 0 deletions lib/Compile/PatchedMacroTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ void PatchedMacroTracker::DoBeginDirective(
ast->marker_offset_to_macro.emplace(
ast->parsed_tokens.last_expansion_begin_offset.value(), directive);

directive->is_command_line = in_command_line;
directive->parsed_begin_index
= ast->parsed_tokens.last_expansion_begin_offset.value();
}
Expand Down Expand Up @@ -2816,6 +2817,22 @@ void PatchedMacroTracker::FileChanged(
clang::SourceLocation loc, clang::PPCallbacks::FileChangeReason reason,
clang::SrcMgr::CharacteristicKind file_type, clang::FileID file_id) {

// Clang has a really annoying way of managing command-line defined macros.
// Instead of associated a specific `FileID` with them, it shoves them in
// as an implicit include into the predefines file ID using file change
// directives.
if (reason == clang::PPCallbacks::EnterFile) {
if (auto user_loc = sm.getPresumedLoc(loc); user_loc.isValid()) {
if (user_loc.getFilename() == llvm::StringRef("<command line>")) {
assert(!in_command_line);
in_command_line = true;
}
}

} else if (reason == clang::PPCallbacks::ExitFile && in_command_line) {
in_command_line = false;
}

// Save off the Clang's current state of the `__COUNTER__` macro back to
// be associated with the last `__COUNTER__` value associated with that
// file hash.
Expand Down
1 change: 1 addition & 0 deletions lib/Compile/PatchedMacroTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class PatchedMacroTracker : public clang::PPCallbacks {
std::vector<std::string> tok_data_vec;
size_t next_tok_data{0};

bool in_command_line{false};

std::vector<MacroNodeImpl *> popped_nodes;

Expand Down

0 comments on commit 0251b26

Please sign in to comment.