Skip to content

Commit

Permalink
Smaller fixes in logging and utils
Browse files Browse the repository at this point in the history
This PR fixes smaller things such as proper display of the log source by inlining the log functions. It also fixes other smaller issues.
  • Loading branch information
oxisto committed Aug 29, 2023
1 parent b06ebc2 commit 1e5c188
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ import de.fraunhofer.aisec.cpg.frontends.TranslationException
import de.fraunhofer.aisec.cpg.graph.Component
import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.helpers.Benchmark
import de.fraunhofer.aisec.cpg.helpers.Util
import de.fraunhofer.aisec.cpg.passes.*
import java.io.File
import java.io.PrintWriter
import java.lang.reflect.InvocationTargetException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.ExecutionException
import java.util.concurrent.atomic.AtomicBoolean
import java.util.stream.Collectors
import kotlin.reflect.full.findAnnotation
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -147,15 +143,13 @@ private constructor(
val list =
sourceLocations.flatMap { file ->
if (file.isDirectory) {
Files.find(
file.toPath(),
999,
{ _: Path?, fileAttr: BasicFileAttributes ->
fileAttr.isRegularFile
}
)
.map { it.toFile() }
.collect(Collectors.toList())
val files =
file
.walkTopDown()
.onEnter { !it.name.startsWith(".") }
.filter { it.isFile && !it.name.startsWith(".") }
.toList()
files
} else {
val frontendClass = file.language?.frontend
val supportsParallelParsing =
Expand All @@ -182,8 +176,8 @@ private constructor(

PrintWriter(tmpFile).use { writer ->
list.forEach {
val cxxExtensions = listOf(".c", ".cpp", ".cc", ".cxx")
if (cxxExtensions.contains(Util.getExtension(it))) {
val cxxExtensions = listOf("c", "cpp", "cc", "cxx")
if (cxxExtensions.contains(it.extension)) {
if (ctx.config.topLevel != null) {
val topLevel = ctx.config.topLevel.toPath()
writer.write(
Expand Down Expand Up @@ -276,7 +270,12 @@ private constructor(
Thread.currentThread().interrupt()
} catch (e: ExecutionException) {
log.error("Error parsing ${futureToFile[future]}", e)
Thread.currentThread().interrupt()
// We previously called Thread.currentThread().interrupt here, however
// it is unsure, why. Therefore, instead of just removing this line, we
// "disabled" it and left this comment here for future generations. If
// we see that it is really not needed we can remove it completely at some
// point.
// Thread.currentThread().interrupt()
}
}

Expand All @@ -298,9 +297,7 @@ private constructor(
val usedFrontends = mutableSetOf<LanguageFrontend<*, *>>()

for (sourceLocation in sourceLocations) {
log.info("Parsing {}", sourceLocation.absolutePath)

var f = parse(component, ctx, sourceLocation)
val f = parse(component, ctx, sourceLocation)
if (f != null) {
handleCompletion(result, usedFrontends, sourceLocation, f)
}
Expand Down Expand Up @@ -331,6 +328,8 @@ private constructor(
ctx: TranslationContext,
sourceLocation: File,
): LanguageFrontend<*, *>? {
log.info("Parsing {}", sourceLocation.absolutePath)

var frontend: LanguageFrontend<*, *>? = null
try {
frontend = getFrontend(sourceLocation, ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.slf4j.LoggerFactory
* @param <T> the raw ast node specific to the parser
* @param <L> the language frontend </L></T></S>
*/
abstract class Handler<ResultNode : Node, HandlerNode, L : LanguageFrontend<HandlerNode, *>>(
abstract class Handler<ResultNode : Node?, HandlerNode, L : LanguageFrontend<HandlerNode, *>>(
protected val configConstructor: Supplier<ResultNode>,
/** Returns the frontend which used this handler. */
val frontend: L
Expand Down Expand Up @@ -140,8 +140,10 @@ abstract class Handler<ResultNode : Node, HandlerNode, L : LanguageFrontend<Hand
ret = configConstructor.get()
}

frontend.process(ctx, ret)
lastNode = ret
if (ret != null) {
frontend.process(ctx, ret)
lastNode = ret
}

return ret
}
Expand Down
68 changes: 26 additions & 42 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration
import de.fraunhofer.aisec.cpg.graph.edge.Properties
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.*
import java.util.function.Function
import java.util.function.Predicate
import org.slf4j.Logger

object Util {
Expand Down Expand Up @@ -149,35 +142,7 @@ object Util {
else refNodes.containsAll(nodeSide)
}

@Throws(IOException::class)
fun inputStreamToString(inputStream: InputStream): String {
ByteArrayOutputStream().use { result ->
val buffer = ByteArray(1024)
var length: Int
while (inputStream.read(buffer).also { length = it } != -1) {
result.write(buffer, 0, length)
}
return result.toString(StandardCharsets.UTF_8)
}
}

@JvmStatic
fun <T> distinctBy(by: Function<in T, *>): Predicate<T> {
val seen = mutableSetOf<Any>()
return Predicate { t: T -> seen.add(by.apply(t)) }
}

fun getExtension(file: File): String {
val pos = file.name.lastIndexOf('.')
return if (pos > 0) {
file.name.substring(pos).lowercase(Locale.getDefault())
} else {
""
}
}

@JvmStatic
fun <AstNode> warnWithFileLocation(
inline fun <AstNode> warnWithFileLocation(
lang: LanguageFrontend<AstNode, *>,
astNode: AstNode,
log: Logger,
Expand All @@ -194,8 +159,7 @@ object Util {
)
}

@JvmStatic
fun <AstNode> errorWithFileLocation(
inline fun <AstNode> errorWithFileLocation(
lang: LanguageFrontend<AstNode, *>,
astNode: AstNode,
log: Logger,
Expand All @@ -212,22 +176,42 @@ object Util {
)
}

@JvmStatic
fun warnWithFileLocation(node: Node, log: Logger, format: String?, vararg arguments: Any?) {
inline fun warnWithFileLocation(
node: Node,
log: Logger,
format: String?,
vararg arguments: Any?
) {
log.warn(
String.format("%s: %s", PhysicalLocation.locationLink(node.location), format),
*arguments
)
}

@JvmStatic
fun errorWithFileLocation(node: Node, log: Logger, format: String?, vararg arguments: Any?) {
inline fun errorWithFileLocation(
node: Node,
log: Logger,
format: String?,
vararg arguments: Any?
) {
log.error(
String.format("%s: %s", PhysicalLocation.locationLink(node.location), format),
*arguments
)
}

inline fun debugWithFileLocation(
node: Node?,
log: Logger,
format: String?,
vararg arguments: Any?
) {
log.debug(
String.format("%s: %s", PhysicalLocation.locationLink(node?.location), format),
*arguments
)
}

/**
* Split a String into multiple parts by using one or more delimiter characters. Any delimiters
* that are surrounded by matching opening and closing brackets are skipped. E.g. "a,(b,c)" will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Expression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.TypeExpression
import de.fraunhofer.aisec.cpg.graph.types.*
import de.fraunhofer.aisec.cpg.helpers.Util.debugWithFileLocation
import java.util.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -70,6 +71,7 @@ class Inference(val start: Node, override val ctx: TranslationContext) :
isStatic: Boolean,
signature: List<Type?>,
returnType: Type?,
hint: CallExpression? = null
): FunctionDeclaration {
// We assume that the start is either a record, a namespace or the translation unit
val record = start as? RecordDeclaration
Expand All @@ -91,7 +93,9 @@ class Inference(val start: Node, override val ctx: TranslationContext) :
newFunctionDeclaration(name ?: "", code)
}

Companion.log.debug(
debugWithFileLocation(
hint,
log,
"Inferred a new {} declaration {} with parameter types {}",
if (inferred is MethodDeclaration) "method" else "function",
inferred.name,
Expand Down Expand Up @@ -361,7 +365,7 @@ class Inference(val start: Node, override val ctx: TranslationContext) :

/**
* This class implements a [HasType.TypeObserver] and uses the observed type to set the
* [ValueDeclaration.declaredType] of a [ValueDeclaration], based on the types we see. It can be
* [ValueDeclaration.type] of a [ValueDeclaration], based on the types we see. It can be
* registered on objects that are used to "start" an inference, for example a
* [MemberExpression], which infers a [FieldDeclaration]. Once the type of the member expression
* becomes known, we can use this information to set the type of the field.
Expand Down Expand Up @@ -429,7 +433,8 @@ fun TranslationUnitDeclaration.inferFunction(
isStatic,
call.signature,
// TODO: Is the call's type the return value's type?
call.type
call.type,
call
)
}

Expand All @@ -446,7 +451,8 @@ fun NamespaceDeclaration.inferFunction(
isStatic,
call.signature,
// TODO: Is the call's type the return value's type?
call.type
call.type,
call
)
}

Expand All @@ -463,6 +469,7 @@ fun RecordDeclaration.inferMethod(
isStatic,
call.signature,
// TODO: Is the call's type the return value's type?
call.type
call.type,
call
) as MethodDeclaration
}

0 comments on commit 1e5c188

Please sign in to comment.