|
| 1 | +//=== CountedRegionsExtractor.cpp - Extracting Of CountedRegions From Code Coverage (Clang) --*- C++ -*===// |
| 2 | +// |
| 3 | +// Traits Static Analyzer (SAPFOR) |
| 4 | +// |
| 5 | +// Copyright 2018 DVM System Group |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +//===---------------------------------------------------------------------===// |
| 20 | +// |
| 21 | +// This file defines a pass to extract & store |
| 22 | +// all the CountedRegions along with their ExecutionCounts |
| 23 | +// from a given code coverage file. |
| 24 | +// |
| 25 | +//===---------------------------------------------------------------------===// |
| 26 | + |
| 27 | +#include "tsar/Analysis/Clang/CountedRegionsExtractor.h" |
| 28 | +#include "tsar/Analysis/Clang/GlobalInfoExtractor.h" |
| 29 | +#include "tsar/Analysis/Clang/NoMacroAssert.h" |
| 30 | +#include "tsar/Core/Query.h" |
| 31 | +#include "tsar/Frontend/Clang/TransformationContext.h" |
| 32 | +#include <clang/AST/Decl.h> |
| 33 | +#include <clang/AST/RecursiveASTVisitor.h> |
| 34 | +#include <clang/AST/Stmt.h> |
| 35 | +#include <clang/Basic/SourceLocation.h> |
| 36 | +#include <llvm/ADT/StringRef.h> |
| 37 | +#include <llvm/IR/Function.h> |
| 38 | +#include <llvm/IR/Module.h> |
| 39 | +#include <llvm/ProfileData/Coverage/CoverageMapping.h> |
| 40 | +#include <llvm/Support/Debug.h> |
| 41 | +#include <llvm/Support/raw_ostream.h> |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +#include <iostream> |
| 45 | + |
| 46 | +using namespace clang; |
| 47 | +using namespace llvm; |
| 48 | +using namespace tsar; |
| 49 | + |
| 50 | +#undef DEBUG_TYPE |
| 51 | +#define DEBUG_TYPE "clang-counted-regions-extractor" |
| 52 | + |
| 53 | +char ClangCountedRegionsExtractor::ID = 0; |
| 54 | + |
| 55 | +INITIALIZE_PASS_IN_GROUP(ClangCountedRegionsExtractor, "clang-counted-regions-extractor", |
| 56 | + "Extracting Of CountedRegions From Code Coverage (Clang)", false, false, |
| 57 | + TransformationQueryManager::getPassRegistry()) |
| 58 | + |
| 59 | +ClangCountedRegionsExtractor::ClangCountedRegionsExtractor() : ImmutablePass(ID) { |
| 60 | + initializeClangCountedRegionsExtractorPass(*PassRegistry::getPassRegistry()); |
| 61 | +} |
| 62 | + |
| 63 | +// Extracts & stores CountedRegions from code coverage |
| 64 | +void ClangCountedRegionsExtractor::initializePass() { |
| 65 | + std::string ObjectFilename{ "/home/alex000/Documents/sapfor-total/working/main" }; |
| 66 | + std::string ProfileFilename{ "/home/alex000/Documents/sapfor-total/working/main.profdata" }; |
| 67 | + |
| 68 | + StringRef ObjectFilenameRef{ ObjectFilename }; |
| 69 | + ArrayRef<StringRef> ObjectFilenameRefs{ ObjectFilenameRef }; |
| 70 | + StringRef ProfileFilenameRef{ ProfileFilename }; |
| 71 | + |
| 72 | + auto CoverageOrErr = coverage::CoverageMapping::load(ObjectFilenameRefs, ProfileFilenameRef); |
| 73 | + if (!CoverageOrErr) { |
| 74 | + errs() << "COVERAGE ERROR\n"; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + LLVM_DEBUG(llvm::dbgs() << "Coverage loading done\n";); |
| 79 | + |
| 80 | + const auto &Coverage = CoverageOrErr->get(); |
| 81 | + |
| 82 | + for (auto &FRI : Coverage->getCoveredFunctions()) { |
| 83 | + for (auto CR : FRI.CountedRegions) { |
| 84 | + CountedRegions.push_back(CR); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + LLVM_DEBUG(llvm::dbgs() << "Stored CountedRegions:\n";); |
| 89 | + for (const auto &CR : CountedRegions) { |
| 90 | + LLVM_DEBUG( |
| 91 | + llvm::dbgs() << "\tstart: " << CR.LineStart << ":" << CR.ColumnStart |
| 92 | + << ", end: " << CR.LineEnd << ":" << CR.ColumnEnd |
| 93 | + << ", count: " << CR.ExecutionCount << "\n"; |
| 94 | + ); |
| 95 | + } |
| 96 | + LLVM_DEBUG(llvm::dbgs() << "\n";); |
| 97 | +} |
| 98 | + |
| 99 | +const std::vector<coverage::CountedRegion> & |
| 100 | +ClangCountedRegionsExtractor::getCountedRegions() const noexcept { |
| 101 | + return CountedRegions; |
| 102 | +} |
| 103 | + |
| 104 | +void ClangCountedRegionsExtractor::getAnalysisUsage(AnalysisUsage &AU) const { |
| 105 | + AU.setPreservesAll(); |
| 106 | +} |
| 107 | + |
| 108 | +ImmutablePass *llvm::createClangCountedRegionsExtractor() { |
| 109 | + return new ClangCountedRegionsExtractor(); |
| 110 | +} |
0 commit comments