diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-01-17 10:39:46 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-01-17 18:09:32 +0000 |
| commit | 38ee454540b9b41d5cc173871dfbf7dd140e8abc (patch) | |
| tree | a7f5f04a7a9286f9eeca01520b51012e3606bb99 /tools/syz-declextract/clangtool/declextract.cpp | |
| parent | 953d1c45a16b7284725e337b47369a8ab111bab4 (diff) | |
pkg/declextract: move const handling logic from the clang tool
Export raw info about consts from the clang tool, and let the Go part handle it.
The less logic is in the clang tool, the better. Also this will allow to remove
unused includes when we know which consts we ended up using.
The more includes we include, the higher the chances we include something
that's broken.
Diffstat (limited to 'tools/syz-declextract/clangtool/declextract.cpp')
| -rw-r--r-- | tools/syz-declextract/clangtool/declextract.cpp | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/tools/syz-declextract/clangtool/declextract.cpp b/tools/syz-declextract/clangtool/declextract.cpp index c41904d8b..4012900d6 100644 --- a/tools/syz-declextract/clangtool/declextract.cpp +++ b/tools/syz-declextract/clangtool/declextract.cpp @@ -113,7 +113,7 @@ private: template <typename T> const T* getResult(StringRef ID) const; FieldType extractRecord(QualType QT, const RecordType* Typ, const std::string& BackupName); std::string extractEnum(const EnumDecl* Decl); - void noteConstUse(const std::string& Name, int64_t Val, const SourceRange& Range); + void emitConst(const std::string& Name, int64_t Val, SourceLocation Loc); std::string getDeclName(const Expr* Expr); const ValueDecl* getValueDecl(const Expr* Expr); std::string getDeclFileID(const Decl* Decl); @@ -304,7 +304,7 @@ std::string Extractor::extractEnum(const EnumDecl* Decl) { std::vector<std::string> Values; for (const auto* Enumerator : Decl->enumerators()) { const std::string& Name = Enumerator->getNameAsString(); - noteConstUse(Name, Enumerator->getInitVal().getExtValue(), Decl->getSourceRange()); + emitConst(Name, Enumerator->getInitVal().getExtValue(), Decl->getBeginLoc()); Values.push_back(Name); } Output.emit(Enum{ @@ -314,19 +314,11 @@ std::string Extractor::extractEnum(const EnumDecl* Decl) { return Name; } -void Extractor::noteConstUse(const std::string& Name, int64_t Val, const SourceRange& Range) { - const std::string& Filename = std::filesystem::relative(SourceManager->getFilename(Range.getBegin()).str()); - // Include only uapi headers. Some ioctl commands defined in internal headers, or even in .c files. - // They have high chances of breaking compilation during const extract. - // If it's not defined in uapi, emit define with concrete value. - // Note: the value may be wrong for other arches. - if (Filename.find("/uapi/") != std::string::npos && Filename.back() == 'h') { - Output.emit(Include{Filename}); - return; - } - Output.emit(Define{ +void Extractor::emitConst(const std::string& Name, int64_t Val, SourceLocation Loc) { + Output.emit(ConstInfo{ .Name = Name, - .Value = std::to_string(Val), + .Filename = std::filesystem::relative(SourceManager->getFilename(Loc).str()), + .Value = Val, }); } @@ -445,8 +437,8 @@ std::vector<std::pair<int, std::string>> Extractor::extractDesignatedInitConsts( for (auto* Match : Matches) { const int64_t Val = *Match->getAPValueResult().getInt().getRawData(); const auto& Name = Match->getEnumConstantDecl()->getNameAsString(); - const auto& SR = Match->getEnumConstantDecl()->getSourceRange(); - noteConstUse(Name, Val, SR); + const auto& Loc = Match->getEnumConstantDecl()->getBeginLoc(); + emitConst(Name, Val, Loc); Inits.emplace_back(Val, Name); } return Inits; @@ -523,7 +515,7 @@ void Extractor::matchNetlinkFamily() { if (!CmdInit) continue; const std::string& OpName = CmdInit->getNameAsString(); - noteConstUse(OpName, CmdInit->getInitVal().getExtValue(), CmdInit->getSourceRange()); + emitConst(OpName, CmdInit->getInitVal().getExtValue(), CmdInit->getBeginLoc()); std::string Policy; if (OpsFields.count("policy") != 0) { if (const auto* PolicyDecl = OpInit->getInit(OpsFields["policy"])->getAsBuiltinConstantDeclRef(*Context)) @@ -818,7 +810,7 @@ std::vector<IoctlCmd> Extractor::extractIoctlCommands(const std::string& Ioctl) if (MacroDef == Macros.end()) continue; int64_t CmdVal = evaluate(Cmd); - noteConstUse(CmdStr, CmdVal, MacroDef->second.SourceRange); + emitConst(CmdStr, CmdVal, MacroDef->second.SourceRange.getBegin()); FieldType CmdType; const auto Dir = _IOC_DIR(CmdVal); if (Dir == _IOC_NONE) { |
