Implement computePreambleBound and collect has_include in PPCallbacks (#44)

This commit is contained in:
ykiko
2025-01-18 20:46:18 +08:00
committed by GitHub
parent f419e7ee20
commit 8cbfcba790
51 changed files with 1350 additions and 1023 deletions

View File

@@ -7,22 +7,21 @@ namespace clice {
namespace {
class PPCallback : public clang::PPCallbacks {
public:
PPCallback(clang::Preprocessor& pp, llvm::DenseMap<clang::FileID, Directive>& directives) :
pp(pp), directives(directives) {}
struct PPCallback : public clang::PPCallbacks {
clang::FileID prevFID;
clang::Preprocessor& PP;
clang::SourceManager& SM;
llvm::DenseMap<clang::FileID, Directive>& directives;
llvm::DenseMap<clang::MacroInfo*, std::size_t> macroCache;
private:
void addInclude(llvm::StringRef path, clang::SourceLocation loc, clang::SourceRange range) {
auto& directive = directives[pp.getSourceManager().getFileID(loc)];
directive.includes.emplace_back(Include{path, loc, range});
}
PPCallback(clang::Preprocessor& PP, llvm::DenseMap<clang::FileID, Directive>& directives) :
PP(PP), SM(PP.getSourceManager()), directives(directives) {}
void addCondition(clang::SourceLocation loc,
Condition::BranchKind kind,
Condition::ConditionValue value,
clang::SourceRange conditionRange) {
auto& directive = directives[pp.getSourceManager().getFileID(loc)];
auto& directive = directives[PP.getSourceManager().getFileID(loc)];
directive.conditions.emplace_back(Condition{kind, value, loc, conditionRange});
}
@@ -61,33 +60,57 @@ private:
}
void addMacro(const clang::MacroInfo* def, MacroRef::Kind kind, clang::SourceLocation loc) {
if(pp.getSourceManager().isWrittenInBuiltinFile(loc) ||
pp.getSourceManager().isWrittenInCommandLineFile(loc)) {
if(PP.getSourceManager().isWrittenInBuiltinFile(loc) ||
PP.getSourceManager().isWrittenInCommandLineFile(loc)) {
return;
}
auto& directive = directives[pp.getSourceManager().getFileID(loc)];
auto& directive = directives[PP.getSourceManager().getFileID(loc)];
directive.macros.emplace_back(MacroRef{def, kind, loc});
}
private:
void FileChanged(clang::SourceLocation loc,
clang::PPCallbacks::FileChangeReason reason,
clang::SrcMgr::CharacteristicKind fileType,
clang::FileID) override {}
/// ============================================================================
/// Rewritten Preprocessor Callbacks
/// ============================================================================
void LexedFileChanged(clang::FileID currFID,
LexedFileChangeReason reason,
clang::SrcMgr::CharacteristicKind,
clang::FileID prevFID,
clang::SourceLocation) override {
if(reason == LexedFileChangeReason::EnterFile && currFID.isValid() && prevFID.isValid() &&
this->prevFID.isValid() && prevFID == this->prevFID) {
directives[prevFID].includes.back().fid = currFID;
}
}
void InclusionDirective(clang::SourceLocation hashLoc,
const clang::Token& includeTok,
llvm::StringRef filename,
bool isAngled,
clang::CharSourceRange filenameRange,
clang::OptionalFileEntryRef file,
llvm::StringRef searchPath,
llvm::StringRef relativePath,
const clang::Module* suggestedModule,
bool moduleImported,
clang::SrcMgr::CharacteristicKind fileType) override {
addInclude(filename, includeTok.getLocation(), filenameRange.getAsRange());
llvm::StringRef,
bool,
clang::CharSourceRange,
clang::OptionalFileEntryRef,
llvm::StringRef,
llvm::StringRef,
const clang::Module*,
bool,
clang::SrcMgr::CharacteristicKind) override {
prevFID = SM.getFileID(hashLoc);
directives[prevFID].includes.emplace_back(Include{
{},
includeTok.getLocation(),
});
}
void HasInclude(clang::SourceLocation location,
llvm::StringRef,
bool,
clang::OptionalFileEntryRef file,
clang::SrcMgr::CharacteristicKind) override {
directives[SM.getFileID(location)].hasIncludes.emplace_back(clice::HasInclude{
file ? file->getName() : "",
location,
});
}
void PragmaDirective(clang::SourceLocation Loc,
@@ -178,11 +201,6 @@ private:
addMacro(def, MacroRef::Undef, name.getLocation());
}
}
private:
clang::Preprocessor& pp;
llvm::DenseMap<clang::FileID, Directive>& directives;
llvm::DenseMap<clang::MacroInfo*, std::size_t> macroCache;
};
} // namespace