Skip to content

Commit be5db4e

Browse files
committed
Improve coverage ; fix CI?
1 parent baf04fa commit be5db4e

8 files changed

Lines changed: 157 additions & 76 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*.profraw
12
__pycache__
23
Testing/
34
*build*

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ set(PROJECT_UNDER_NAME "type_correct")
3030
# C++ standard
3131
set(CMAKE_CXX_STANDARD 17)
3232

33+
# Test configuration (needs to be available before subdirectories are added).
34+
option(BUILD_TESTS "Build tests" ON)
35+
include(CTest)
36+
if (NOT BUILD_TESTS)
37+
set(BUILD_TESTING OFF CACHE BOOL "Build tests" FORCE)
38+
endif ()
39+
3340
# Visibility Settings
3441
# -------------------
3542
# Hide symbols by default to reduce binary size and collision probability,
@@ -249,8 +256,6 @@ configure_file(
249256
add_subdirectory("${PROJECT_UNDER_NAME}")
250257

251258
# Add Tests (Optional)
252-
option(BUILD_TESTS "Build tests" ON)
253-
include(CTest)
254259
if (BUILD_TESTING)
255260
add_subdirectory("${PROJECT_UNDER_NAME}/tests")
256261
endif (BUILD_TESTING)

type_correct/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(LIBRARY_NAME "${PROJECT_UNDER_NAME}")
2020
# Variable: Header_Files
2121
# Description: List of public headers exposed by the library.
2222
set(Header_Files
23+
"ClangCompat.h"
2324
"TypeCorrect.h"
2425
"StructAnalyzer.h"
2526
"TypeSolver.h"
@@ -159,4 +160,4 @@ install(EXPORT "${LIBRARY_NAME}Targets" DESTINATION "${CMAKE_INSTALL_DATADIR}/${
159160

160161
# Redundant explicit install for verification (keeps original logic intact).
161162
install(FILES "${Header_Files}" "${Cli_Header_Files}" "${_export_file}"
162-
TYPE "INCLUDE")
163+
TYPE "INCLUDE")

type_correct/ClangCompat.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef TYPE_CORRECT_CLANG_COMPAT_H
2+
#define TYPE_CORRECT_CLANG_COMPAT_H
3+
4+
#include <clang/AST/Expr.h>
5+
#include <clang/AST/Type.h>
6+
#include <llvm/Config/llvm-config.h>
7+
8+
namespace type_correct::clang_compat {
9+
10+
constexpr clang::ExprValueKind PrValueKind() {
11+
#if LLVM_VERSION_MAJOR >= 15
12+
return clang::VK_PRValue;
13+
#else
14+
return clang::VK_RValue;
15+
#endif
16+
}
17+
18+
constexpr clang::TagTypeKind StructTagKind() {
19+
#if LLVM_VERSION_MAJOR >= 18
20+
return clang::TagTypeKind::Struct;
21+
#else
22+
return clang::TTK_Struct;
23+
#endif
24+
}
25+
26+
} // namespace type_correct::clang_compat
27+
28+
#endif // TYPE_CORRECT_CLANG_COMPAT_H

type_correct/TypeCorrect.cpp

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "TypeCorrect.h"
9+
#include "ClangCompat.h"
910

1011
#include <algorithm>
1112
#include <clang/AST/ASTContext.h>
@@ -626,11 +627,6 @@ std::vector<type_correct::ChangeRecord> TypeCorrectMatcher::GetChanges() const {
626627
//------------------------------------------------------------------------------
627628
void TypeCorrectMatcher::run(
628629
const MatchFinder::MatchResult &Result) {
629-
630-
ASTContext *Ctx = Result.Context;
631-
if (!Ctx)
632-
return;
633-
634630
if (const auto *Cast =
635631
Result.Nodes.getNodeAs<ExplicitCastExpr>("explicit_cast")) {
636632
ExplicitCasts.push_back(Cast);
@@ -649,6 +645,8 @@ void TypeCorrectMatcher::ProcessRedundantCasts(ASTContext &Ctx) {
649645
if (!Cast || !Seen.insert(Cast).second)
650646
continue;
651647

648+
const Expr *SubExpr = Cast->getSubExpr();
649+
652650
SourceLocation Begin = Cast->getBeginLoc();
653651
SourceLocation End = Cast->getEndLoc();
654652
if (Begin.isInvalid() || End.isInvalid())
@@ -658,29 +656,36 @@ void TypeCorrectMatcher::ProcessRedundantCasts(ASTContext &Ctx) {
658656
if (SM.getFileID(Begin) != SM.getMainFileID())
659657
continue;
660658

661-
const Expr *SubExpr = Cast->getSubExpr();
662-
if (!SubExpr)
663-
continue;
664-
665659
SourceLocation SubBegin = SubExpr->getBeginLoc();
666660
SourceLocation SubEnd = SubExpr->getEndLoc();
667661
if (SubBegin.isInvalid() || SubEnd.isInvalid())
668662
continue;
669663
if (SubBegin.isMacroID() || SubEnd.isMacroID())
670664
continue;
671665

672-
QualType CastType = NormalizeType(Cast->getTypeAsWritten());
666+
const TypeSourceInfo *CastTypeInfo = Cast->getTypeInfoAsWritten();
667+
if (!CastTypeInfo)
668+
continue;
669+
QualType CastType = NormalizeType(CastTypeInfo->getType());
673670
QualType SubType = NormalizeType(SubExpr->getType());
674671
if (CastType.isNull() || SubType.isNull())
675672
continue;
676673
if (!Ctx.hasSameType(CastType, SubType))
677674
continue;
678675

679-
std::string SubText =
680-
Lexer::getSourceText(
681-
CharSourceRange::getTokenRange(SubExpr->getSourceRange()), SM,
682-
LangOpts)
683-
.str();
676+
bool InvalidBuffer = false;
677+
llvm::StringRef Buffer = SM.getBufferData(SM.getFileID(SubBegin),
678+
&InvalidBuffer);
679+
if (InvalidBuffer)
680+
continue;
681+
682+
std::string SubText;
683+
if (!Buffer.empty()) {
684+
SubText = Lexer::getSourceText(
685+
CharSourceRange::getTokenRange(SubExpr->getSourceRange()),
686+
SM, LangOpts)
687+
.str();
688+
}
684689
if (SubText.empty())
685690
continue;
686691

@@ -722,8 +727,7 @@ void TypeCorrectMatcher::onEndOfTranslationUnit(ASTContext &Ctx) {
722727
SourceManager &SM = Rewriter.getSourceMgr();
723728

724729
if (!InPlace) {
725-
if (const llvm::RewriteBuffer *Buf =
726-
Rewriter.getRewriteBufferFor(SM.getMainFileID())) {
730+
if (const auto *Buf = Rewriter.getRewriteBufferFor(SM.getMainFileID())) {
727731
Buf->write(llvm::outs());
728732
} else {
729733
llvm::outs() << SM.getBufferData(SM.getMainFileID());
@@ -775,25 +779,33 @@ void TypeCorrectASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
775779
#ifdef TYPE_CORRECT_TEST
776780
namespace type_correct::test_support {
777781

778-
TypeLoc GetBaseTypeLocForTest(TypeLoc TL) { return GetBaseTypeLoc(TL); }
782+
TYPE_CORRECT_EXPORT TypeLoc GetBaseTypeLocForTest(TypeLoc TL) {
783+
return GetBaseTypeLoc(TL);
784+
}
779785

780-
QualType NormalizeTypeForTest(QualType T) { return NormalizeType(T); }
786+
TYPE_CORRECT_EXPORT QualType NormalizeTypeForTest(QualType T) {
787+
return NormalizeType(T);
788+
}
781789

782-
QualType GetWiderTypeForTest(QualType A, QualType B, ASTContext &Ctx) {
790+
TYPE_CORRECT_EXPORT QualType GetWiderTypeForTest(QualType A, QualType B,
791+
ASTContext &Ctx) {
783792
return GetWiderType(A, B, Ctx);
784793
}
785794

786-
std::string TypeToStringForTest(QualType T, ASTContext &Ctx) {
795+
TYPE_CORRECT_EXPORT std::string TypeToStringForTest(QualType T,
796+
ASTContext &Ctx) {
787797
return TypeToString(T, Ctx);
788798
}
789799

790-
const NamedDecl *ResolveNamedDeclForTest(const Expr *E) {
800+
TYPE_CORRECT_EXPORT const NamedDecl *ResolveNamedDeclForTest(const Expr *E) {
791801
return ResolveNamedDecl(E);
792802
}
793803

794-
bool IsIdentifierForTest(llvm::StringRef Text) { return IsIdentifier(Text); }
804+
TYPE_CORRECT_EXPORT bool IsIdentifierForTest(llvm::StringRef Text) {
805+
return IsIdentifier(Text);
806+
}
795807

796-
void CoverVisitorEdgeCases(ASTContext &Ctx) {
808+
TYPE_CORRECT_EXPORT void CoverVisitorEdgeCases(ASTContext &Ctx) {
797809
StructAnalyzer Engine(true, true, "");
798810
llvm::DenseMap<const NamedDecl *, DeclUpdate> DeclUpdates;
799811
llvm::DenseMap<const VarDecl *, TemplateArgUpdate> TemplateUpdates;
@@ -829,16 +841,16 @@ void CoverVisitorEdgeCases(ASTContext &Ctx) {
829841
&NewId, Ctx.IntTy, Ctx.getTrivialTypeSourceInfo(Ctx.IntTy), SC_None);
830842
Visitor.TestUpdateDeclType(NewDecl, Literal);
831843

832-
auto *Opaque =
833-
new (Ctx) OpaqueValueExpr(SourceLocation(), QualType(), VK_RValue);
844+
auto *Opaque = new (Ctx)
845+
OpaqueValueExpr(SourceLocation(), Ctx.IntTy,
846+
type_correct::clang_compat::PrValueKind());
834847
Visitor.TestUpdateDeclType(VD, Opaque);
835848
}
836849

837-
void CoverEnsureTemplateArgUpdateEdges(ASTContext &Ctx,
838-
const VarDecl *TemplateDecl,
839-
const VarDecl *NonTemplateDecl,
840-
const VarDecl *NonTypeTemplateDecl,
841-
VarDecl *TrivialTemplateDecl) {
850+
TYPE_CORRECT_EXPORT void CoverEnsureTemplateArgUpdateEdges(
851+
ASTContext &Ctx, const VarDecl *TemplateDecl,
852+
const VarDecl *NonTemplateDecl, const VarDecl *NonTypeTemplateDecl,
853+
VarDecl *TrivialTemplateDecl) {
842854
StructAnalyzer Engine(true, true, "");
843855
llvm::DenseMap<const NamedDecl *, DeclUpdate> DeclUpdates;
844856
llvm::DenseMap<const VarDecl *, TemplateArgUpdate> TemplateUpdates;
@@ -869,7 +881,8 @@ void CoverEnsureTemplateArgUpdateEdges(ASTContext &Ctx,
869881
}
870882
}
871883

872-
void CoverTemplateUpdateMapEdges(ASTContext &Ctx, Rewriter &Rewriter) {
884+
TYPE_CORRECT_EXPORT void CoverTemplateUpdateMapEdges(ASTContext &Ctx,
885+
Rewriter &Rewriter) {
873886
llvm::DenseMap<const NamedDecl *, DeclUpdate> DeclUpdates;
874887
llvm::DenseMap<const VarDecl *, TemplateArgUpdate> TemplateUpdates;
875888
std::vector<type_correct::ChangeRecord> Changes;
@@ -915,7 +928,8 @@ void CoverTemplateUpdateMapEdges(ASTContext &Ctx, Rewriter &Rewriter) {
915928
TemplateUpdates);
916929
}
917930

918-
void CoverDeclUpdateMapEdges(ASTContext &Ctx, Rewriter &Rewriter) {
931+
TYPE_CORRECT_EXPORT void CoverDeclUpdateMapEdges(ASTContext &Ctx,
932+
Rewriter &Rewriter) {
919933
llvm::DenseMap<const NamedDecl *, DeclUpdate> DeclUpdates;
920934
std::vector<type_correct::ChangeRecord> Changes;
921935
SourceManager &SM = Rewriter.getSourceMgr();
@@ -962,7 +976,8 @@ void CoverDeclUpdateMapEdges(ASTContext &Ctx, Rewriter &Rewriter) {
962976
(void)CollectMacroUpdates(Rewriter, true, Changes, Ctx, DeclUpdates);
963977
}
964978

965-
bool CoverMacroScannerEdges(ASTContext &Ctx, Rewriter &Rewriter) {
979+
TYPE_CORRECT_EXPORT bool CoverMacroScannerEdges(ASTContext &Ctx,
980+
Rewriter &Rewriter) {
966981
SourceManager &SM = Rewriter.getSourceMgr();
967982
FileID MainFile = SM.getMainFileID();
968983
auto EntryRef = SM.getFileEntryRefForID(MainFile);
@@ -994,7 +1009,8 @@ bool CoverMacroScannerEdges(ASTContext &Ctx, Rewriter &Rewriter) {
9941009
return true;
9951010
}
9961011

997-
void CoverRecordChangeEdges(ASTContext &Ctx, Rewriter &Rewriter) {
1012+
TYPE_CORRECT_EXPORT void CoverRecordChangeEdges(ASTContext &Ctx,
1013+
Rewriter &Rewriter) {
9981014
std::vector<type_correct::ChangeRecord> Changes;
9991015
SourceManager &SM = Rewriter.getSourceMgr();
10001016

type_correct/TypeSolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ std::map<const NamedDecl *, NodeState> TypeSolver::Solve(ASTContext *Ctx) {
356356
Updates[Node] = State;
357357
}
358358
}
359-
360359
return Updates;
361360
}
362361

@@ -430,4 +429,4 @@ void TypeSolver::ProcessSCC(const std::vector<const NamedDecl *> &SCC,
430429
}
431430
}
432431

433-
} // namespace type_correct
432+
} // namespace type_correct

type_correct/TypeSolver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace type_correct {
3030
* @struct ValueRange
3131
* @brief Represents a closed numerical interval [Min, Max].
3232
*/
33-
struct ValueRange {
33+
struct TYPE_CORRECT_EXPORT ValueRange {
3434
int64_t Min; ///< Minimum observed value.
3535
int64_t Max; ///< Maximum observed value.
3636
bool HasMin; ///< Flag indicating Min is valid.

0 commit comments

Comments
 (0)