2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
42 #include <type_traits>
43 #ifdef HAVE_ADT_OWNINGPTR_H
44 #include <llvm/ADT/OwningPtr.h>
48 #ifdef HAVE_LLVM_OPTION_ARG_H
49 #include <llvm/Option/Arg.h>
51 #include <llvm/Support/raw_ostream.h>
52 #include <llvm/Support/ManagedStatic.h>
53 #include <llvm/Support/MemoryBuffer.h>
54 #ifdef HAVE_TARGETPARSER_HOST_H
55 #include <llvm/TargetParser/Host.h>
57 #include <llvm/Support/Host.h>
59 #include <clang/Basic/Version.h>
60 #include <clang/Basic/Builtins.h>
61 #include <clang/Basic/FileSystemOptions.h>
62 #include <clang/Basic/FileManager.h>
63 #include <clang/Basic/TargetOptions.h>
64 #include <clang/Basic/TargetInfo.h>
65 #include <clang/Driver/Compilation.h>
66 #include <clang/Driver/Driver.h>
67 #include <clang/Driver/Tool.h>
68 #include <clang/Frontend/CompilerInstance.h>
69 #include <clang/Frontend/CompilerInvocation.h>
70 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
71 #include <clang/Basic/DiagnosticOptions.h>
73 #include <clang/Frontend/DiagnosticOptions.h>
75 #include <clang/Frontend/TextDiagnosticPrinter.h>
76 #ifdef HAVE_LEX_HEADERSEARCHOPTIONS_H
77 #include <clang/Lex/HeaderSearchOptions.h>
79 #include <clang/Frontend/HeaderSearchOptions.h>
81 #ifdef HAVE_CLANG_BASIC_LANGSTANDARD_H
82 #include <clang/Basic/LangStandard.h>
84 #include <clang/Frontend/LangStandard.h>
86 #ifdef HAVE_LEX_PREPROCESSOROPTIONS_H
87 #include <clang/Lex/PreprocessorOptions.h>
89 #include <clang/Frontend/PreprocessorOptions.h>
91 #include <clang/Frontend/FrontendOptions.h>
92 #include <clang/Frontend/Utils.h>
93 #include <clang/Lex/HeaderSearch.h>
94 #include <clang/Lex/Preprocessor.h>
95 #include <clang/Lex/Pragma.h>
96 #include <clang/AST/ASTContext.h>
97 #include <clang/AST/ASTConsumer.h>
98 #include <clang/Sema/Sema.h>
99 #include <clang/Sema/SemaDiagnostic.h>
100 #include <clang/Parse/Parser.h>
101 #include <clang/Parse/ParseAST.h>
104 #include <isl/constraint.h>
108 #include "clang_compatibility.h"
114 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
117 using namespace clang
;
118 using namespace clang::driver
;
119 #ifdef HAVE_LLVM_OPTION_ARG_H
120 using namespace llvm::opt
;
123 #ifdef HAVE_ADT_OWNINGPTR_H
124 #define unique_ptr llvm::OwningPtr
127 /* Called if we found something we didn't expect in one of the pragmas.
128 * We'll provide more informative warnings later.
130 static void unsupported(Preprocessor
&PP
, SourceLocation loc
)
132 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
133 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
135 DiagnosticBuilder B
= diag
.Report(loc
, id
);
138 static int get_int(const char *s
)
140 return s
[0] == '"' ? atoi(s
+ 1) : atoi(s
);
143 static ValueDecl
*get_value_decl(Sema
&sema
, Token
&token
)
145 IdentifierInfo
*name
;
148 if (token
.isNot(tok::identifier
))
151 name
= token
.getIdentifierInfo();
152 decl
= sema
.LookupSingleName(sema
.TUScope
, name
,
153 token
.getLocation(), Sema::LookupOrdinaryName
);
154 return decl
? cast_or_null
<ValueDecl
>(decl
) : NULL
;
157 /* Handle pragmas of the form
159 * #pragma value_bounds identifier lower_bound upper_bound
161 * For each such pragma, add a mapping
162 * { identifier[] -> [i] : lower_bound <= i <= upper_bound }
165 struct PragmaValueBoundsHandler
: public PragmaHandler
{
168 isl_union_map
*value_bounds
;
170 PragmaValueBoundsHandler(isl_ctx
*ctx
, Sema
&sema
) :
171 PragmaHandler("value_bounds"), sema(sema
), ctx(ctx
) {
172 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
173 value_bounds
= isl_union_map_empty(space
);
176 ~PragmaValueBoundsHandler() {
177 isl_union_map_free(value_bounds
);
180 virtual void HandlePragma(Preprocessor
&PP
,
181 PragmaIntroducer Introducer
,
192 vd
= get_value_decl(sema
, token
);
194 unsupported(PP
, token
.getLocation());
199 if (!token
.isLiteral()) {
200 unsupported(PP
, token
.getLocation());
204 lb
= get_int(token
.getLiteralData());
207 if (!token
.isLiteral()) {
208 unsupported(PP
, token
.getLocation());
212 ub
= get_int(token
.getLiteralData());
214 dim
= isl_space_alloc(ctx
, 0, 0, 1);
215 map
= isl_map_universe(dim
);
216 map
= isl_map_lower_bound_si(map
, isl_dim_out
, 0, lb
);
217 map
= isl_map_upper_bound_si(map
, isl_dim_out
, 0, ub
);
218 id
= isl_id_alloc(ctx
, vd
->getName().str().c_str(), vd
);
219 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
221 value_bounds
= isl_union_map_add_map(value_bounds
, map
);
225 /* Given a variable declaration, check if it has an integer initializer
226 * and if so, add a parameter corresponding to the variable to "value"
227 * with its value fixed to the integer initializer and return the result.
229 static __isl_give isl_set
*extract_initialization(__isl_take isl_set
*value
,
241 vd
= cast
<VarDecl
>(decl
);
244 if (!vd
->getType()->isIntegerType())
246 expr
= vd
->getInit();
249 il
= cast
<IntegerLiteral
>(expr
);
253 ctx
= isl_set_get_ctx(value
);
254 id
= isl_id_alloc(ctx
, vd
->getName().str().c_str(), vd
);
255 space
= isl_space_params_alloc(ctx
, 1);
256 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0, id
);
257 set
= isl_set_universe(space
);
259 v
= PetScan::extract_int(ctx
, il
);
260 set
= isl_set_fix_val(set
, isl_dim_param
, 0, v
);
262 return isl_set_intersect(value
, set
);
265 /* Handle pragmas of the form
267 * #pragma parameter identifier lower_bound
269 * #pragma parameter identifier lower_bound upper_bound
271 * For each such pragma, intersect the context with the set
272 * [identifier] -> { [] : lower_bound <= identifier <= upper_bound }
274 struct PragmaParameterHandler
: public PragmaHandler
{
277 isl_set
*&context_value
;
279 PragmaParameterHandler(Sema
&sema
, isl_set
*&context
,
280 isl_set
*&context_value
) :
281 PragmaHandler("parameter"), sema(sema
), context(context
),
282 context_value(context_value
) {}
284 virtual void HandlePragma(Preprocessor
&PP
,
285 PragmaIntroducer Introducer
,
288 isl_ctx
*ctx
= isl_set_get_ctx(context
);
298 vd
= get_value_decl(sema
, token
);
300 unsupported(PP
, token
.getLocation());
305 if (!token
.isLiteral()) {
306 unsupported(PP
, token
.getLocation());
310 lb
= get_int(token
.getLiteralData());
313 if (token
.isLiteral()) {
315 ub
= get_int(token
.getLiteralData());
316 } else if (token
.isNot(tok::eod
)) {
317 unsupported(PP
, token
.getLocation());
321 id
= isl_id_alloc(ctx
, vd
->getName().str().c_str(), vd
);
322 dim
= isl_space_params_alloc(ctx
, 1);
323 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
325 set
= isl_set_universe(dim
);
327 set
= isl_set_lower_bound_si(set
, isl_dim_param
, 0, lb
);
329 set
= isl_set_upper_bound_si(set
, isl_dim_param
, 0, ub
);
331 context
= isl_set_intersect(context
, set
);
333 context_value
= extract_initialization(context_value
, vd
);
337 /* Handle pragmas of the form
339 * #pragma pencil independent
341 * For each such pragma, add an entry to the "independent" vector.
343 struct PragmaPencilHandler
: public PragmaHandler
{
344 std::vector
<Independent
> &independent
;
346 PragmaPencilHandler(std::vector
<Independent
> &independent
) :
347 PragmaHandler("pencil"), independent(independent
) {}
349 virtual void HandlePragma(Preprocessor
&PP
,
350 PragmaIntroducer Introducer
,
353 IdentifierInfo
*info
;
356 if (token
.isNot(tok::identifier
))
359 info
= token
.getIdentifierInfo();
360 if (!info
->isStr("independent"))
364 if (token
.isNot(tok::eod
))
367 SourceManager
&SM
= PP
.getSourceManager();
368 SourceLocation sloc
= PencilTok
.getLocation();
369 unsigned line
= SM
.getExpansionLineNumber(sloc
);
370 independent
.push_back(Independent(line
));
374 #ifdef HAVE_TRANSLATELINECOL
376 /* Return a SourceLocation for line "line", column "col" of file "FID".
378 SourceLocation
translateLineCol(SourceManager
&SM
, FileID FID
, unsigned line
,
381 return SM
.translateLineCol(FID
, line
, col
);
386 /* Return a SourceLocation for line "line", column "col" of file "FID".
388 SourceLocation
translateLineCol(SourceManager
&SM
, FileID FID
, unsigned line
,
391 return SM
.getLocation(SM
.getFileEntryForID(FID
), line
, col
);
396 /* List of pairs of #pragma scop and #pragma endscop locations.
399 std::vector
<ScopLoc
> list
;
401 /* Add a new start (#pragma scop) location to the list.
402 * If the last #pragma scop did not have a matching
403 * #pragma endscop then overwrite it.
404 * "start" points to the location of the scop pragma.
406 void add_start(SourceManager
&SM
, SourceLocation start
) {
410 int line
= SM
.getExpansionLineNumber(start
);
411 start
= translateLineCol(SM
, SM
.getFileID(start
), line
, 1);
412 loc
.start_line
= line
;
413 loc
.start
= SM
.getFileOffset(start
);
414 if (list
.size() == 0 || list
[list
.size() - 1].end
!= 0)
417 list
[list
.size() - 1] = loc
;
420 /* Set the end location (#pragma endscop) of the last pair
422 * If there is no such pair of if the end of that pair
423 * is already set, then ignore the spurious #pragma endscop.
424 * "end" points to the location of the endscop pragma.
426 void add_end(SourceManager
&SM
, SourceLocation end
) {
427 if (list
.size() == 0 || list
[list
.size() - 1].end
!= 0)
429 list
[list
.size() - 1].endscop
= end
;
430 int line
= SM
.getExpansionLineNumber(end
);
431 end
= translateLineCol(SM
, SM
.getFileID(end
), line
+ 1, 1);
432 list
[list
.size() - 1].end
= SM
.getFileOffset(end
);
436 /* Handle pragmas of the form
440 * In particular, store the location of the line containing
441 * the pragma in the list "scops".
443 struct PragmaScopHandler
: public PragmaHandler
{
446 PragmaScopHandler(ScopLocList
&scops
) :
447 PragmaHandler("scop"), scops(scops
) {}
449 virtual void HandlePragma(Preprocessor
&PP
,
450 PragmaIntroducer Introducer
,
452 SourceManager
&SM
= PP
.getSourceManager();
453 SourceLocation sloc
= ScopTok
.getLocation();
454 scops
.add_start(SM
, sloc
);
458 /* Handle pragmas of the form
462 * In particular, store the location of the line following the one containing
463 * the pragma in the list "scops".
465 struct PragmaEndScopHandler
: public PragmaHandler
{
468 PragmaEndScopHandler(ScopLocList
&scops
) :
469 PragmaHandler("endscop"), scops(scops
) {}
471 virtual void HandlePragma(Preprocessor
&PP
,
472 PragmaIntroducer Introducer
,
474 SourceManager
&SM
= PP
.getSourceManager();
475 SourceLocation sloc
= EndScopTok
.getLocation();
476 scops
.add_end(SM
, sloc
);
480 /* Handle pragmas of the form
482 * #pragma live-out identifier, identifier, ...
484 * Each identifier on the line is stored in live_out.
486 struct PragmaLiveOutHandler
: public PragmaHandler
{
488 set
<ValueDecl
*> &live_out
;
490 PragmaLiveOutHandler(Sema
&sema
, set
<ValueDecl
*> &live_out
) :
491 PragmaHandler("live"), sema(sema
), live_out(live_out
) {}
493 virtual void HandlePragma(Preprocessor
&PP
,
494 PragmaIntroducer Introducer
,
499 if (token
.isNot(tok::minus
))
502 if (token
.isNot(tok::identifier
) ||
503 !token
.getIdentifierInfo()->isStr("out"))
507 while (token
.isNot(tok::eod
)) {
510 vd
= get_value_decl(sema
, token
);
512 unsupported(PP
, token
.getLocation());
517 if (token
.is(tok::comma
))
523 /* For each array in "scop", set its value_bounds property
524 * based on the information in "value_bounds" and
525 * mark it as live_out if it appears in "live_out".
527 static void update_arrays(struct pet_scop
*scop
,
528 __isl_take isl_union_map
*value_bounds
, set
<ValueDecl
*> &live_out
)
530 set
<ValueDecl
*>::iterator lo_it
;
531 isl_ctx
*ctx
= isl_union_map_get_ctx(value_bounds
);
534 isl_union_map_free(value_bounds
);
538 for (int i
= 0; i
< scop
->n_array
; ++i
) {
543 pet_array
*array
= scop
->arrays
[i
];
545 id
= isl_set_get_tuple_id(array
->extent
);
546 decl
= pet_id_get_decl(id
);
548 space
= isl_space_alloc(ctx
, 0, 0, 1);
549 space
= isl_space_set_tuple_id(space
, isl_dim_in
, id
);
551 bounds
= isl_union_map_extract_map(value_bounds
, space
);
552 if (!isl_map_plain_is_empty(bounds
))
553 array
->value_bounds
= isl_map_range(bounds
);
555 isl_map_free(bounds
);
557 lo_it
= live_out
.find(decl
);
558 if (lo_it
!= live_out
.end())
562 isl_union_map_free(value_bounds
);
565 /* Extract a pet_scop (if any) from each appropriate function.
566 * Each detected scop is passed to "fn".
567 * When autodetecting, at most one scop is extracted from each function.
568 * If "function" is not NULL, then we only extract a pet_scop if the
569 * name of the function matches.
570 * If "autodetect" is false, then we only extract if we have seen
571 * scop and endscop pragmas and if these are situated inside the function
574 struct PetASTConsumer
: public ASTConsumer
{
576 ASTContext
&ast_context
;
577 DiagnosticsEngine
&diags
;
579 std::vector
<Independent
> independent
;
580 const char *function
;
581 pet_options
*options
;
584 isl_set
*context_value
;
585 set
<ValueDecl
*> live_out
;
586 PragmaValueBoundsHandler
*vb_handler
;
587 isl_stat (*fn
)(struct pet_scop
*scop
, void *user
);
591 PetASTConsumer(isl_ctx
*ctx
, Preprocessor
&PP
, ASTContext
&ast_context
,
592 DiagnosticsEngine
&diags
, ScopLocList
&scops
,
593 const char *function
, pet_options
*options
,
594 isl_stat (*fn
)(struct pet_scop
*scop
, void *user
), void *user
) :
595 PP(PP
), ast_context(ast_context
), diags(diags
),
596 scops(scops
), function(function
), options(options
),
598 vb_handler(NULL
), fn(fn
), user(user
), error(false)
601 space
= isl_space_params_alloc(ctx
, 0);
602 context
= isl_set_universe(isl_space_copy(space
));
603 context_value
= isl_set_universe(space
);
607 isl_set_free(context
);
608 isl_set_free(context_value
);
611 void handle_value_bounds(Sema
*sema
) {
612 vb_handler
= new PragmaValueBoundsHandler(ctx
, *sema
);
613 PP
.AddPragmaHandler(vb_handler
);
616 /* Add all pragma handlers to this->PP.
617 * The pencil pragmas are only handled if the pencil option is set.
619 void add_pragma_handlers(Sema
*sema
) {
620 PP
.AddPragmaHandler(new PragmaParameterHandler(*sema
, context
,
622 if (options
->pencil
) {
624 PH
= new PragmaPencilHandler(independent
);
625 PP
.AddPragmaHandler(PH
);
627 handle_value_bounds(sema
);
630 __isl_give isl_union_map
*get_value_bounds() {
631 return isl_union_map_copy(vb_handler
->value_bounds
);
634 /* Pass "scop" to "fn" after performing some postprocessing.
635 * In particular, add the context and value_bounds constraints
636 * speficied through pragmas, add reference identifiers and
637 * reset user pointers on parameters and tuple ids.
639 * If "scop" does not contain any statements and autodetect
640 * is turned on, then skip it.
642 void call_fn(pet_scop
*scop
) {
647 if (diags
.hasErrorOccurred()) {
652 if (options
->autodetect
&& scop
->n_stmt
== 0) {
656 scop
->context
= isl_set_intersect(scop
->context
,
657 isl_set_copy(context
));
658 scop
->context_value
= isl_set_intersect(scop
->context_value
,
659 isl_set_copy(context_value
));
661 update_arrays(scop
, get_value_bounds(), live_out
);
663 scop
= pet_scop_add_ref_ids(scop
);
664 scop
= pet_scop_anonymize(scop
);
666 if (fn(scop
, user
) < 0)
670 /* For each explicitly marked scop (using pragmas),
671 * extract the scop and call "fn" on it if it is inside "fd".
673 void scan_scops(FunctionDecl
*fd
) {
675 vector
<ScopLoc
>::iterator it
;
676 isl_union_map
*vb
= vb_handler
->value_bounds
;
677 SourceManager
&SM
= PP
.getSourceManager();
680 if (scops
.list
.size() == 0)
683 start
= SM
.getFileOffset(begin_loc(fd
));
684 end
= SM
.getFileOffset(end_loc(fd
));
686 for (it
= scops
.list
.begin(); it
!= scops
.list
.end(); ++it
) {
694 PetScan
ps(PP
, ast_context
, fd
, loc
, options
,
695 isl_union_map_copy(vb
), independent
);
701 virtual HandleTopLevelDeclReturn
HandleTopLevelDecl(DeclGroupRef dg
) {
702 DeclGroupRef::iterator it
;
705 return HandleTopLevelDeclContinue
;
707 for (it
= dg
.begin(); it
!= dg
.end(); ++it
) {
708 isl_union_map
*vb
= vb_handler
->value_bounds
;
709 FunctionDecl
*fd
= dyn_cast
<clang::FunctionDecl
>(*it
);
715 fd
->getNameInfo().getAsString() != function
)
717 if (options
->autodetect
) {
720 PetScan
ps(PP
, ast_context
, fd
, loc
, options
,
721 isl_union_map_copy(vb
),
732 return HandleTopLevelDeclContinue
;
736 static const char *ResourceDir
=
737 CLANG_PREFIX
"/lib/clang/" CLANG_VERSION_STRING
;
739 static const char *implicit_functions
[] = {
740 "min", "max", "intMod", "intCeil", "intFloor", "ceild", "floord"
742 static const char *pencil_implicit_functions
[] = {
743 "imin", "umin", "imax", "umax", "__pencil_kill"
746 /* Should "ident" be treated as an implicit function?
747 * If "pencil" is set, then also allow pencil specific builtins.
749 static bool is_implicit(const IdentifierInfo
*ident
, int pencil
)
751 const char *name
= ident
->getNameStart();
752 for (size_t i
= 0; i
< ARRAY_SIZE(implicit_functions
); ++i
)
753 if (!strcmp(name
, implicit_functions
[i
]))
757 for (size_t i
= 0; i
< ARRAY_SIZE(pencil_implicit_functions
); ++i
)
758 if (!strcmp(name
, pencil_implicit_functions
[i
]))
763 /* Ignore implicit function declaration warnings on
764 * "min", "max", "ceild" and "floord" as we detect and handle these
766 * If "pencil" is set, then also ignore them on pencil specific
769 struct MyDiagnosticPrinter
: public TextDiagnosticPrinter
{
770 const DiagnosticOptions
*DiagOpts
;
772 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
773 MyDiagnosticPrinter(DiagnosticOptions
*DO
, int pencil
) :
774 TextDiagnosticPrinter(llvm::errs(), DO
), pencil(pencil
) {}
775 virtual DiagnosticConsumer
*clone(DiagnosticsEngine
&Diags
) const {
776 return new MyDiagnosticPrinter(&Diags
.getDiagnosticOptions(),
780 MyDiagnosticPrinter(const DiagnosticOptions
&DO
, int pencil
) :
781 DiagOpts(&DO
), TextDiagnosticPrinter(llvm::errs(), DO
),
783 virtual DiagnosticConsumer
*clone(DiagnosticsEngine
&Diags
) const {
784 return new MyDiagnosticPrinter(*DiagOpts
, pencil
);
787 virtual void HandleDiagnostic(DiagnosticsEngine::Level level
,
788 const DiagnosticInfo
&info
) {
789 if (info
.getID() == diag::ext_implicit_function_decl_c99
&&
790 info
.getNumArgs() >= 1 &&
791 info
.getArgKind(0) == DiagnosticsEngine::ak_identifierinfo
&&
792 is_implicit(info
.getArgIdentifier(0), pencil
))
793 /* ignore warning */;
795 TextDiagnosticPrinter::HandleDiagnostic(level
, info
);
801 #ifdef HAVE_CXXISPRODUCTION
802 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
804 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
805 "", false, false, Diags
);
807 #elif defined(HAVE_ISPRODUCTION)
808 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
810 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
813 #elif defined(DRIVER_CTOR_TAKES_DEFAULTIMAGENAME)
814 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
816 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(),
820 static Driver
*construct_driver(const char *binary
, DiagnosticsEngine
&Diags
)
822 return new Driver(binary
, llvm::sys::getDefaultTargetTriple(), Diags
);
826 namespace clang
{ namespace driver
{ class Job
; } }
828 /* Clang changed its API from 3.5 to 3.6 and once more in 3.7.
829 * We fix this with a simple overloaded function here.
832 static Job
*command(Job
*J
) { return J
; }
833 static Job
*command(Job
&J
) { return &J
; }
834 static Command
*command(Command
&C
) { return &C
; }
837 #ifdef CREATE_FROM_ARGS_TAKES_ARRAYREF
839 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
840 * In this case, an ArrayRef<const char *>.
842 static void create_from_args(CompilerInvocation
&invocation
,
843 const ArgStringList
*args
, DiagnosticsEngine
&Diags
)
845 CompilerInvocation::CreateFromArgs(invocation
, *args
, Diags
);
850 /* Call CompilerInvocation::CreateFromArgs with the right arguments.
851 * In this case, two "const char *" pointers.
853 static void create_from_args(CompilerInvocation
&invocation
,
854 const ArgStringList
*args
, DiagnosticsEngine
&Diags
)
856 CompilerInvocation::CreateFromArgs(invocation
, args
->data() + 1,
857 args
->data() + args
->size(),
863 /* Create a CompilerInvocation object that stores the command line
864 * arguments constructed by the driver.
865 * The arguments are mainly useful for setting up the system include
866 * paths on newer clangs and on some platforms.
868 static CompilerInvocation
*construct_invocation(const char *filename
,
869 DiagnosticsEngine
&Diags
)
871 const char *binary
= CLANG_PREFIX
"/bin/clang";
872 const unique_ptr
<Driver
> driver(construct_driver(binary
, Diags
));
873 std::vector
<const char *> Argv
;
874 Argv
.push_back(binary
);
875 Argv
.push_back(filename
);
876 const unique_ptr
<Compilation
> compilation(
877 driver
->BuildCompilation(llvm::ArrayRef
<const char *>(Argv
)));
878 JobList
&Jobs
= compilation
->getJobs();
882 Command
*cmd
= cast
<Command
>(ClangAPI::command(*Jobs
.begin()));
883 if (strcmp(cmd
->getCreator().getName(), "clang"))
886 const ArgStringList
*args
= &cmd
->getArguments();
888 CompilerInvocation
*invocation
= new CompilerInvocation
;
889 create_from_args(*invocation
, args
, Diags
);
895 static CompilerInvocation
*construct_invocation(const char *filename
,
896 DiagnosticsEngine
&Diags
)
903 #ifdef HAVE_BASIC_DIAGNOSTICOPTIONS_H
905 static MyDiagnosticPrinter
*construct_printer(CompilerInstance
*Clang
,
908 return new MyDiagnosticPrinter(new DiagnosticOptions(), pencil
);
913 static MyDiagnosticPrinter
*construct_printer(CompilerInstance
*Clang
,
916 return new MyDiagnosticPrinter(Clang
->getDiagnosticOpts(), pencil
);
921 #ifdef CREATETARGETINFO_TAKES_SHARED_PTR
923 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
924 DiagnosticsEngine
&Diags
)
926 shared_ptr
<TargetOptions
> TO
= Clang
->getInvocation().TargetOpts
;
927 TO
->Triple
= llvm::sys::getDefaultTargetTriple();
928 return TargetInfo::CreateTargetInfo(Diags
, TO
);
931 #elif defined(CREATETARGETINFO_TAKES_POINTER)
933 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
934 DiagnosticsEngine
&Diags
)
936 TargetOptions
&TO
= Clang
->getTargetOpts();
937 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
938 return TargetInfo::CreateTargetInfo(Diags
, &TO
);
943 static TargetInfo
*create_target_info(CompilerInstance
*Clang
,
944 DiagnosticsEngine
&Diags
)
946 TargetOptions
&TO
= Clang
->getTargetOpts();
947 TO
.Triple
= llvm::sys::getDefaultTargetTriple();
948 return TargetInfo::CreateTargetInfo(Diags
, TO
);
953 #ifdef CREATEDIAGNOSTICS_TAKES_ARG
955 static void create_diagnostics(CompilerInstance
*Clang
)
957 Clang
->createDiagnostics(0, NULL
);
962 static void create_diagnostics(CompilerInstance
*Clang
)
964 Clang
->createDiagnostics();
969 #ifdef CREATEPREPROCESSOR_TAKES_TUKIND
971 static void create_preprocessor(CompilerInstance
*Clang
)
973 Clang
->createPreprocessor(TU_Complete
);
978 static void create_preprocessor(CompilerInstance
*Clang
)
980 Clang
->createPreprocessor();
985 #ifdef ADDPATH_TAKES_4_ARGUMENTS
987 void add_path(HeaderSearchOptions
&HSO
, string Path
)
989 HSO
.AddPath(Path
, frontend::Angled
, false, false);
994 void add_path(HeaderSearchOptions
&HSO
, string Path
)
996 HSO
.AddPath(Path
, frontend::Angled
, true, false, false);
1001 #ifdef HAVE_SETMAINFILEID
1003 template <typename T
>
1004 static void create_main_file_id(SourceManager
&SM
, const T
&file
)
1006 SM
.setMainFileID(SM
.createFileID(file
, SourceLocation(),
1012 static void create_main_file_id(SourceManager
&SM
, const FileEntry
*file
)
1014 SM
.createMainFileID(file
);
1019 #ifdef SETLANGDEFAULTS_TAKES_5_ARGUMENTS
1021 #include "set_lang_defaults_arg4.h"
1023 static void set_lang_defaults(CompilerInstance
*Clang
)
1025 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
1026 TargetOptions
&TO
= Clang
->getTargetOpts();
1027 llvm::Triple
T(TO
.Triple
);
1028 SETLANGDEFAULTS::setLangDefaults(Clang
->getLangOpts(), IK_C
, T
,
1029 setLangDefaultsArg4(PO
),
1030 LangStandard::lang_unspecified
);
1035 static void set_lang_defaults(CompilerInstance
*Clang
)
1037 CompilerInvocation::setLangDefaults(Clang
->getLangOpts(), IK_C
,
1038 LangStandard::lang_unspecified
);
1043 #ifdef SETINVOCATION_TAKES_SHARED_PTR
1045 static void set_invocation(CompilerInstance
*Clang
,
1046 CompilerInvocation
*invocation
)
1048 Clang
->setInvocation(std::shared_ptr
<CompilerInvocation
>(invocation
));
1053 static void set_invocation(CompilerInstance
*Clang
,
1054 CompilerInvocation
*invocation
)
1056 Clang
->setInvocation(invocation
);
1061 /* Helper function for ignore_error that only gets enabled if T
1062 * (which is either const FileEntry * or llvm::ErrorOr<const FileEntry *>)
1063 * has getError method, i.e., if it is llvm::ErrorOr<const FileEntry *>.
1066 static const FileEntry
*ignore_error_helper(const T obj
, int,
1067 int[1][sizeof(obj
.getError())])
1072 /* Helper function for ignore_error that is always enabled,
1073 * but that only gets selected if the variant above is not enabled,
1074 * i.e., if T is const FileEntry *.
1077 static const FileEntry
*ignore_error_helper(const T obj
, long, void *)
1082 /* Given either a const FileEntry * or a llvm::ErrorOr<const FileEntry *>,
1083 * extract out the const FileEntry *.
1086 static const FileEntry
*ignore_error(const T obj
)
1088 return ignore_error_helper(obj
, 0, NULL
);
1091 /* This is identical to std::void_t in C++17.
1093 template< class... >
1094 using void_t
= void;
1096 /* A template class with value true if "T" has a getFileRef method.
1098 template <class T
, class = void>
1099 struct HasGetFileRef
: public std::false_type
{};
1101 struct HasGetFileRef
<T
, ::void_t
<decltype(&T::getFileRef
)>> :
1102 public std::true_type
{};
1104 /* Return the FileEntryRef/FileEntry corresponding to the given file name or
1105 * an error if anything went wrong.
1107 * If T (= FileManager) has a getFileRef method, then call that and
1108 * return a FileEntryRef.
1109 * Otherwise, call getFile and return a FileEntry (pointer).
1111 template <typename T
,
1112 typename
std::enable_if
<HasGetFileRef
<T
>::value
, bool>::type
= true>
1113 static auto getFile(T
& obj
, const std::string
&filename
)
1114 -> llvm::ErrorOr
<decltype(*obj
.getFileRef(filename
))>
1116 auto file
= obj
.getFileRef(filename
);
1118 return std::error_code();
1121 template <typename T
,
1122 typename
std::enable_if
<!HasGetFileRef
<T
>::value
, bool>::type
= true>
1123 static llvm::ErrorOr
<const FileEntry
*> getFile(T
& obj
,
1124 const std::string
&filename
)
1126 const FileEntry
*file
= ignore_error(obj
.getFile(filename
));
1128 return std::error_code();
1132 /* Return the ownership of "buffer".
1134 * If "buffer" is a pointer, simply return the pointer.
1135 * If "buffer" is a std::unique_ptr, call release() on it.
1136 * Note that std::unique_ptr was not being used back when clang
1137 * was still using llvm::OwningPtr.
1139 static llvm::MemoryBuffer
*release(llvm::MemoryBuffer
*buffer
)
1143 #ifndef HAVE_ADT_OWNINGPTR_H
1144 static llvm::MemoryBuffer
*release(std::unique_ptr
<llvm::MemoryBuffer
> buffer
)
1146 return buffer
.release();
1150 /* Pencil specific predefines.
1152 static const char *pencil_predefines
=
1153 "void __pencil_assume(int assumption);\n"
1154 "#define pencil_access(f) annotate(\"pencil_access(\" #f \")\")\n";
1156 /* Add pet specific predefines to the preprocessor.
1157 * Currently, these are all pencil specific, so they are only
1158 * added if "pencil" is set.
1160 * Include a special "/pet" header and ensure it gets replaced
1161 * by "pencil_predefines" by mapping the "/pet" file to a memory buffer.
1163 static void add_predefines(PreprocessorOptions
&PO
, int pencil
)
1168 PO
.Includes
.push_back("/pet");
1169 PO
.addRemappedFile("/pet",
1170 release(llvm::MemoryBuffer::getMemBuffer(pencil_predefines
)));
1173 /* Do not treat implicit function declaration warnings as errors.
1175 * Only do this if DiagnosticsEngine::setDiagnosticGroupWarningAsError
1176 * is available. In earlier versions of clang, these warnings
1177 * are not treated as errors by default.
1179 #ifdef HAVE_SET_DIAGNOSTIC_GROUP_WARNING_AS_ERROR
1180 static void set_implicit_function_declaration_no_error(DiagnosticsEngine
&Diags
)
1182 Diags
.setDiagnosticGroupWarningAsError("implicit-function-declaration",
1186 static void set_implicit_function_declaration_no_error(DiagnosticsEngine
&Diags
)
1191 /* Extract a pet_scop from each function in the C source file called "filename".
1192 * Each detected scop is passed to "fn".
1193 * If "function" is not NULL, only extract a pet_scop from the function
1195 * If "autodetect" is set, extract any pet_scop we can find.
1196 * Otherwise, extract the pet_scop from the region delimited
1197 * by "scop" and "endscop" pragmas.
1199 * We first set up the clang parser and then try to extract the
1200 * pet_scop from the appropriate function(s) in PetASTConsumer.
1202 static isl_stat
foreach_scop_in_C_source(isl_ctx
*ctx
,
1203 const char *filename
, const char *function
, pet_options
*options
,
1204 isl_stat (*fn
)(struct pet_scop
*scop
, void *user
), void *user
)
1206 CompilerInstance
*Clang
= new CompilerInstance();
1207 create_diagnostics(Clang
);
1208 DiagnosticsEngine
&Diags
= Clang
->getDiagnostics();
1209 Diags
.setSuppressSystemWarnings(true);
1210 set_implicit_function_declaration_no_error(Diags
);
1211 TargetInfo
*target
= create_target_info(Clang
, Diags
);
1212 Clang
->setTarget(target
);
1213 set_lang_defaults(Clang
);
1214 CompilerInvocation
*invocation
= construct_invocation(filename
, Diags
);
1216 set_invocation(Clang
, invocation
);
1217 Diags
.setClient(construct_printer(Clang
, options
->pencil
));
1218 Clang
->createFileManager();
1219 Clang
->createSourceManager(Clang
->getFileManager());
1220 HeaderSearchOptions
&HSO
= Clang
->getHeaderSearchOpts();
1221 HSO
.ResourceDir
= ResourceDir
;
1222 for (int i
= 0; i
< options
->n_path
; ++i
)
1223 add_path(HSO
, options
->paths
[i
]);
1224 PreprocessorOptions
&PO
= Clang
->getPreprocessorOpts();
1225 for (int i
= 0; i
< options
->n_define
; ++i
)
1226 PO
.addMacroDef(options
->defines
[i
]);
1227 add_predefines(PO
, options
->pencil
);
1228 create_preprocessor(Clang
);
1229 Preprocessor
&PP
= Clang
->getPreprocessor();
1230 PP
.getBuiltinInfo().initializeBuiltins(PP
.getIdentifierTable(),
1235 auto file
= getFile(Clang
->getFileManager(), filename
);
1237 isl_die(ctx
, isl_error_unknown
, "unable to open file",
1238 do { delete Clang
; return isl_stat_error
; } while (0));
1239 create_main_file_id(Clang
->getSourceManager(), *file
);
1241 Clang
->createASTContext();
1242 PetASTConsumer
consumer(ctx
, PP
, Clang
->getASTContext(), Diags
,
1243 scops
, function
, options
, fn
, user
);
1244 Sema
*sema
= new Sema(PP
, Clang
->getASTContext(), consumer
);
1246 if (!options
->autodetect
) {
1247 PP
.AddPragmaHandler(new PragmaScopHandler(scops
));
1248 PP
.AddPragmaHandler(new PragmaEndScopHandler(scops
));
1249 PP
.AddPragmaHandler(new PragmaLiveOutHandler(*sema
,
1250 consumer
.live_out
));
1253 consumer
.add_pragma_handlers(sema
);
1255 Diags
.getClient()->BeginSourceFile(Clang
->getLangOpts(), &PP
);
1257 Diags
.getClient()->EndSourceFile();
1262 return consumer
.error
? isl_stat_error
: isl_stat_ok
;
1265 /* Extract a pet_scop from each function in the C source file called "filename".
1266 * Each detected scop is passed to "fn".
1268 * This wrapper around foreach_scop_in_C_source is mainly used to ensure
1269 * that all objects on the stack (of that function) are destroyed before we
1270 * call llvm_shutdown.
1272 static isl_stat
pet_foreach_scop_in_C_source(isl_ctx
*ctx
,
1273 const char *filename
, const char *function
,
1274 isl_stat (*fn
)(struct pet_scop
*scop
, void *user
), void *user
)
1277 pet_options
*options
;
1278 bool allocated
= false;
1280 options
= isl_ctx_peek_pet_options(ctx
);
1282 options
= pet_options_new_with_defaults();
1286 r
= foreach_scop_in_C_source(ctx
, filename
, function
, options
,
1288 llvm::llvm_shutdown();
1291 pet_options_free(options
);
1296 /* Store "scop" into the address pointed to by "user".
1297 * Return -1 to indicate that we are not interested in any further scops.
1298 * This function should therefore not be called a second call
1299 * so in principle there is no need to check if we have already set *user.
1301 static isl_stat
set_first_scop(pet_scop
*scop
, void *user
)
1303 pet_scop
**p
= (pet_scop
**) user
;
1308 pet_scop_free(scop
);
1310 return isl_stat_error
;
1313 /* Extract a pet_scop from the C source file called "filename".
1314 * If "function" is not NULL, extract the pet_scop from the function
1317 * We start extracting scops from every function and then abort
1318 * as soon as we have extracted one scop.
1320 struct pet_scop
*pet_scop_extract_from_C_source(isl_ctx
*ctx
,
1321 const char *filename
, const char *function
)
1323 pet_scop
*scop
= NULL
;
1325 pet_foreach_scop_in_C_source(ctx
, filename
, function
,
1326 &set_first_scop
, &scop
);
1331 /* Internal data structure for pet_transform_C_source
1333 * transform is the function that should be called to print a scop
1334 * in is the input source file
1335 * out is the output source file
1336 * end is the offset of the end of the previous scop (zero if we have not
1337 * found any scop yet)
1338 * p is a printer that prints to out.
1340 struct pet_transform_data
{
1341 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
1342 struct pet_scop
*scop
, void *user
);
1351 /* This function is called each time a scop is detected.
1353 * We first copy the input text code from the end of the previous scop
1354 * until the start of "scop" and then print the scop itself through
1355 * a call to data->transform. We set up the printer to print
1356 * the transformed code with the same (initial) indentation as
1357 * the original code.
1358 * Finally, we keep track of the end of "scop" so that we can
1359 * continue copying when we find the next scop.
1361 * Before calling data->transform, we store a pointer to the original
1362 * input file in the extended scop in case the user wants to call
1363 * pet_scop_print_original from the callback.
1365 static isl_stat
pet_transform(struct pet_scop
*scop
, void *user
)
1367 struct pet_transform_data
*data
= (struct pet_transform_data
*) user
;
1371 return isl_stat_error
;
1372 start
= pet_loc_get_start(scop
->loc
);
1373 if (copy(data
->in
, data
->out
, data
->end
, start
) < 0)
1375 data
->end
= pet_loc_get_end(scop
->loc
);
1376 scop
= pet_scop_set_input_file(scop
, data
->in
);
1377 data
->p
= isl_printer_set_indent_prefix(data
->p
,
1378 pet_loc_get_indent(scop
->loc
));
1379 data
->p
= data
->transform(data
->p
, scop
, data
->user
);
1381 return isl_stat_error
;
1384 pet_scop_free(scop
);
1385 return isl_stat_error
;
1388 /* Transform the C source file "input" by rewriting each scop
1389 * through a call to "transform".
1390 * When autodetecting scops, at most one scop per function is rewritten.
1391 * The transformed C code is written to "output".
1393 * For each scop we find, we first copy the input text code
1394 * from the end of the previous scop (or the beginning of the file
1395 * in case of the first scop) until the start of the scop
1396 * and then print the scop itself through a call to "transform".
1397 * At the end we copy everything from the end of the final scop
1398 * until the end of the input file to "output".
1400 int pet_transform_C_source(isl_ctx
*ctx
, const char *input
, FILE *out
,
1401 __isl_give isl_printer
*(*transform
)(__isl_take isl_printer
*p
,
1402 struct pet_scop
*scop
, void *user
), void *user
)
1404 struct pet_transform_data data
;
1409 if (input
&& strcmp(input
, "-")) {
1410 data
.in
= fopen(input
, "r");
1412 isl_die(ctx
, isl_error_unknown
, "unable to open file",
1416 data
.p
= isl_printer_to_file(ctx
, data
.out
);
1417 data
.p
= isl_printer_set_output_format(data
.p
, ISL_FORMAT_C
);
1419 data
.transform
= transform
;
1422 r
= pet_foreach_scop_in_C_source(ctx
, input
, NULL
,
1423 &pet_transform
, &data
);
1425 isl_printer_free(data
.p
);
1428 if (r
== 0 && copy(data
.in
, data
.out
, data
.end
, -1) < 0)
1431 if (data
.in
!= stdin
)