29
29
// ===----------------------------------------------------------------------===//
30
30
31
31
#include " CHERI/CHERIUtils.h"
32
+ #include < clang/ASTMatchers/ASTMatchFinder.h>
32
33
#include " clang/ASTMatchers/ASTMatchers.h"
33
34
#include " clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
34
- #include " clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
35
- #include < clang/ASTMatchers/ASTMatchFinder.h>
36
35
#include < clang/StaticAnalyzer/Core/BugReporter/BugType.h>
37
36
#include < clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h>
37
+ #include " clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
38
38
39
39
using namespace clang ;
40
40
using namespace ento ;
@@ -425,11 +425,18 @@ void printAlign(raw_ostream &OS, unsigned TZC) {
425
425
OS << " )" ;
426
426
}
427
427
428
- void describeOriginalAllocation (const MemRegion *MR, PathSensitiveBugReport &W,
429
- const SourceManager &SM,
428
+ const DeclRegion *getOriginalAllocation (const MemRegion *MR) {
429
+ if (const DeclRegion *DR = MR->getAs <DeclRegion>())
430
+ return DR;
431
+ if (const ElementRegion *ER = MR->getAs <ElementRegion>())
432
+ return getOriginalAllocation (ER->getSuperRegion ());
433
+ return nullptr ;
434
+ }
435
+
436
+ void describeOriginalAllocation (const ValueDecl *SrcDecl,
437
+ PathDiagnosticLocation SrcLoc,
438
+ PathSensitiveBugReport &W,
430
439
ASTContext &ASTCtx) {
431
- if (const DeclRegion *DR = MR->getAs <DeclRegion>()) {
432
- const ValueDecl *SrcDecl = DR->getDecl ();
433
440
SmallString<350 > Note;
434
441
llvm::raw_svector_ostream OS2 (Note);
435
442
const QualType &AllocType = SrcDecl->getType ().getCanonicalType ();
@@ -438,9 +445,7 @@ void describeOriginalAllocation(const MemRegion *MR, PathSensitiveBugReport &W,
438
445
OS2 << " which has an alignment requirement " ;
439
446
OS2 << ASTCtx.getTypeAlignInChars (AllocType).getQuantity ();
440
447
OS2 << " bytes" ;
441
- W.addNote (Note, PathDiagnosticLocation::create (SrcDecl, SM));
442
- } else if (const ElementRegion *ER = MR->getAs <ElementRegion>())
443
- describeOriginalAllocation (ER->getSuperRegion (), W, SM, ASTCtx);
448
+ W.addNote (Note, SrcLoc);
444
449
}
445
450
446
451
} // namespace
@@ -467,16 +472,27 @@ PointerAlignmentChecker::emitCastAlignWarn(
467
472
OS << " alignment " << DstReqAlign;
468
473
OS << " bytes" ;
469
474
475
+ const SVal &SrcVal = C.getSVal (CE->getSubExpr ());
476
+ const ValueDecl *MRDecl = nullptr ;
477
+ PathDiagnosticLocation MRDeclLoc;
478
+ if (const MemRegion *MR = SrcVal.getAsRegion ()) {
479
+ if (const DeclRegion *OriginalAlloc = getOriginalAllocation (MR)) {
480
+ MRDecl = OriginalAlloc->getDecl ();
481
+ MRDeclLoc = PathDiagnosticLocation::create (MRDecl, C.getSourceManager ());
482
+ }
483
+ }
484
+
470
485
auto W = std::make_unique<PathSensitiveBugReport>(
471
- DstAlignIsCap ? *CapCastAlignBug : *CastAlignBug, ErrorMessage, ErrNode);
472
- W->addRange (CE->getSourceRange ());
486
+ DstAlignIsCap ? *CapCastAlignBug : *CastAlignBug,
487
+ ErrorMessage, ErrNode,
488
+ MRDeclLoc, MRDecl);
473
489
474
- const SVal &SrcVal = C.getSVal (CE->getSubExpr ());
475
490
W->markInteresting (SrcVal);
476
491
if (SymbolRef S = SrcVal.getAsSymbol ())
477
492
W->addVisitor (std::make_unique<AlignmentBugVisitor>(S));
478
- else if (const MemRegion *MR = SrcVal.getAsRegion ()) {
479
- describeOriginalAllocation (MR, *W, C.getSourceManager (), C.getASTContext ());
493
+
494
+ if (MRDecl) {
495
+ describeOriginalAllocation (MRDecl, MRDeclLoc, *W, C.getASTContext ());
480
496
}
481
497
482
498
C.emitReport (std::move (W));
0 commit comments